Unit04 - OOP in Java
Unit04 - OOP in Java
Instructor:
!"#$%&'()*+&&,*",-).$#)
/)0,"",#)',/#%1%&)/%-)
+%-,#*"/%-1%& $.)"21*)
Study and understand Study and understand
all the artifacts 3$+#*,4 the self study topics
Principles of OOP
v Mobile class:
§ Blackberry class
Encapsulation
v “number” is private, code from outside this class cannot access the
variable directly:
v Instead, the outside code have to invoke the getter, getNumber() and
the setter, setNumber() in order to read or update the variable, for
example:
Output:
Constructor with 2 params!
Constructor with 4 params!
Samsung Galaxy S9
• Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
• Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
• Public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Inheritance
v Terms:
P Class: A class is a group of objects which have common properties.
P Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
P Super Class/Parent Class: Superclass is the class from where a subclass inherits
the features. It is also called a base class or a parent class.
P Reusability: As the name specifies, reusability is a mechanism which facilitates you to
reuse the fields and methods of the existing class when you create a new class. You
can use the same fields and methods already defined in the previous class.
Subclass
Single inheritance
ü Inherits from one superclass
v Multiple inheritance:
ü Inherits from multiple superclasses
• Java does not support multiple inheritance in classes
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 30
Inheritance Example
Color
v Examples:
class Building {
Building() {
System.out.print("b ");
}
Building(String name) {
this();
System.out.print("bn " + name);
}
} public class House extends Building {
House() {
System.out.print("h ");
}
House(String name) {
this();
System.out.print("hn " + name);
}
• Implementation dependent
– subclass methods more likely dependent on superclass
implementation
– superclass implementation changes may result in subclass
modifications