Software Construction and Design
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
SOFT2201/COMP9201
Software Construction and Design I
Week 2: OO Theory in Java
Four Pillars of OOP
Java Object Oriented Programming
• Object-oriented programming (OOP) is a programming paradigm based on the
concept of objects, which can contain data and code
(https://en.wikipedia.org/wiki/Object-oriented_programming).
• Classes and Objects are the two main aspects of Java OOP
• Class is a blueprint or a template for an object
• An object is an instance of a class
• Both class and object can have data and code part
• Data and code belong to a class should be declared using a static keyword
• The data part belong to an object is referred to as fields or instance variables;
• The code part is referred to as methods.
19
Four pillars/principles of OOP
• Encapsulation: Bundles the data and operations on that data into a single unit.
Hiding the internal state and some functionality of an object from the outside
world.
• Inheritance: Allows one class to inherits data and methods from another. Enabling
the reuse of code and the establishment of hierarchical relationships.
• Abstraction: Hide complex implementation details and only show essential
features, reducing complexity and enhancing understanding.
• Polymorphism: Allows objects to take on different forms or have multiple
behaviors, enabling flexibility and versatility in programming.
20
Encapsulation in Java
Key Elements
• In Java, encapsulation is about data protection and controlled access.
• Key elements of encapsulation in Java:
• Private Access Modifier
• The class fields should be declared as private
• They are only accessible within that class
• Public Access Modifier
• Some methods can be declared as public
• These methods can be accessed from anywhere in the program.
• Getter and Setter methods
• Provide public methods to access and modify private fields
• Allow controlled access to data while maintaining encapsulation
22
Encapsulation Example
• The field name has a private access
modifier
• The getName method returns the
value of the variable name.
• The setName method takes a
parameter (newName) and assigns it
to the name variable.
• Both getter and setter are declared
as public
• The this keyword is used to refer to
the current object.
23
public class Person {
// private = restricted access
private String name;
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
Encapsulation Example (cont’d)
24
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name variable to "John”
System.out.println(myObj.getName());
}
}
An object of the Person class is created
using a system provided default constructor,
the field value is not specified
The print method uses the
getter method to obtain the
field value
The field value is
assigned using
the setter
method
public class Person {
// private = restricted access
private String name;
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
Benefits of encapsulation
• Control: Using encapsulation, we can add conditions to control how data is
accessed or modified.
• Flexibility and Maintenance: By encapsulating data, any internal changes to
a class won't directly affect its interactions with other classes.
• Increased Security: Shielding class members and only allowing them to be
changed through controlled methods ensures security.
• Modular Approach: Encapsulation allows a system to be split into clear, well-
defined modules, which can then be developed and maintained separately.
25
Inheritance in Java
Key element and features
• Inheritance allows programmers to create new classes (subclasses) by
inheriting properties and methods from existing classes (superclasses). This
promotes code reusability and hierarchical relationships between classes.
• Java supports inheritance through the extends keyword.
• subclass extends superclass
• Java supports single inheritance, meaning a class can extend only one
superclass.
• Multiple subclasses can inherit from a single superclass.
• Subclass inherits all accessible fields and methods from its superclass.
27
Inheritance of data members
• A subclass inherits all non-private data members from its superclass.
• Any field declared as public, protected, or with default access (package-private) in the
superclass becomes available to the subclass.
• Data members declared as private in the superclass are not accessible to the subclass.