03_OOP_2-LT
03_OOP_2-LT
❑ Benefits of inheritance :
❖ Reusability of code – reusability
designability .
1. Inheritance in Java
❑ In Java, only single inheritance is supported but not multiple
inheritance. Multiple inheritance in Java is designed based
on the concept of interface.
ChildClass ChildClass_1
ChildClass_1 ChildClass_2
1) Single
ChildClass_2 3) Hierarchical
2) MultiLevel
4
1. Inheritance in Java
Syntax for creating an inherited class in Java :
public ParentClass {
…
}
public ChildClass extends ParentClass {
…
}
Note: access to the properties and methods of the parent class depends
on the access scope defined
5
1. Inheritance in Java
public ParentClass {
…
}
public ChildClass_1 extends ParentClass { 2) MultiLevel
…
}
public ChildClass_2 extends ChildClass_1 {
…
}
public ParentClass {
…
}
public ChildClass_1 extends ParentClass { 3) Hierarchical
…
}
public ChildClass_2 extends ParentClass {
…
}
6
1. Inheritance in Java – Example
public class Parent { ❑ Single Inheritance
public void color (){
System.out.println(“blue”);}
}
public class Child extends Parent {
public void show (){
System.out.print(“This is an apple ”);}
}
public class Test {
public static void main(String args[]) {
Child c = new Child ();
c. show ();
c. color ();
Result This is a green apple
}
} 7
1. Inheritance in Java
❑ MultiLevel
public class Parent { public class Test {
public void color (){ public static void main(String
System.out.println(“ blue ”);} args[]) {
} Child2 c2 = new Child2 ();
c2. show1 ();
public class Child1 extends Parent {
c2. color ();
public void show1 (){
c2. show2 ();
System.out.print(“ This is an apple ”);}
}
}
}
public class Child2 extends Child1 {
public void show2 (){
System.out.println(“ Made in Vietnam ”);}
} This is a green apple
Result
Made in Vietnam
8
1. Inheritance in Java
❑ Hierarchical
public class Parent { public class Test {
public void color(){ public static void main(String
System.out.println(“blue”);} args[]) {
} Child1 c1 = new Child1 ();
c1. show1 ();
public class Child1 extends Parent { c1. color ();
public void show1(){
System.out.print(“This is an apple ”);} Child2 c2 = new Child2 ();
} c2. show2 ();
c2. color ();
public class Child2 extends Parent { }
public void show2(){ }
System.out.print(“This is an orange”);}
This is a green apple
} Result
This is a green orange.
9
1. Inheritance in Java
Some keywords used:
❑ final:
❖ class: not allow creating subclasses derived from this class.
❖ method : not allow child class to override parent class method
10
1. Inheritance in Java
Constructor in inheritance
• Jaw create Are not Okay excess plan
• Jaw create belong to right subclass call next jaw create belong to
parent class via super(<ds reference ) number >)
• Word call next jaw create other Right To be sentence command
head fairy in constructor
public class Student extends Person{
public String studentId ;
• In school fit parent class has jaw create wear determine then
Are not need call one way explicit
11
1. Inheritance in Java – Example
1. Build a 2D point layer with the following methods:
▪ Show point coordinates
▪ Calculate the distance from the point to the origin
2. Build a 3D point class that inherits the 2D point class with the method:
▪ Show point coordinates
▪ Calculate the distance from the point to the origin
3. Build the main program:
▪ Create 2D point list and 3D point list
▪ Calculate the sum of the distances of 2D points to the origin
▪ Calculate the sum of the distances of 3D points to the origin
12
1. Inheritance in Java
ParentClass ChildClass
Point2D Point3D
• attributes •attributes
ox o x, y
oy oz
• constructors •constructors
o getX(), setX() •getX(), getY()
o getY(), setY() •setX(), setY()
• methods: •getZ(), setZ()
(x,y) o show() •methods:
o distance() oshow() (x,y,z)
𝑑= 𝑥2 + 𝑦2
odistance() 𝑑= 𝑥2 + 𝑦2 + 𝑧2
13
1. Inheritance in Java – Example
public class Point2D {
private double x,y ; attributes
public Point2D (double x , double y ){
this.x = x ;
this.y = y ; constructor
}
public double getX (){
return x ;
}
public void setX (double x ){
this.x = x ; getters and setters
}
public double getY (){
return y ;
}
public void setY (double y ){
this.y = y ;
} methods
public String show (){
return "Point2D ["+ this.x +","+ this.y +"]";
}
public double distance (){
return Math.sqrt( this.x * this.x+this.y * this.y );
}
} 14
1. Inheritance in Java – Example
public class Point3D extends Point2D { Add new attribute
private double z ;
public Point3D (double x, double y, double z){
super(x,y);
this.z =z;
} Redefine the
constructor to match
public double getZ() { the subclass
return z ;
} add getters and setters for new
public void setZ (double z){ properties of subclass
this.z =z;
}
redefine
@Override
methods
public String show() {
return "Point3D [" + this.getX()+ "," + this.getY()+ "," + this.z
+"]";
}
@Override
public double distance() {
return Math.sqrt(super.distance() * super.distance() + this.z * this.z
);
}
} 15
1. Inheritance in Java – Example
public static void main(String[] args) {
List<Point2D> listPoint2D =new ArrayList<Point2D>();
Point2D p1_2D = new Point2D(1.0,4.0);
Point2D p2_2D = new Point2D(3.0,6.0);
Point2D p3_2D = new Point2D(4.0,7.0);
//add elements to the list
listPoint2D.add(p1_2D);
listPoint2D.add(p2_2D);
listPoint2D.add(p3_2D);
for(Point2D point:listPoint2D){
System.out.println(point.show());
}
//distance calculation
double sum2D=0.0;
for(Point2D point:listPoint2D){
sum2D +=point.distance();
}
System.out.println("Distance of Point2D: " +sum2D);
}
Result:
Point2D [1.0,4.0]
Point2D [3.0,6.0]
Point2D [4.0,7.0]
Distance of Point2D: 18.89356730641558 16
1. Inheritance in Java – Example
List<Point3D> listPoint3D =new ArrayList<Point3D >();
Point3D p1_3D = new Point3D(1.0,4.0,7.0);
Point3D p2_3D = new Point3D(3.0,6.0,9.0);
Point3D p3_3D = new Point3D(5.0,7.0,3.0);
//add elements to the list
listPoint3D.add(p1_3D);
listPoint3D.add(p2_3D);
listPoint3D.add(p3_3D);
for(Point3D point:listPoint3D){
System.out.println(point.show());
}
//distance calculation
double sum3D=0.0;
for(Point3D point:listPoint3D){
sum3D +=point.distance();
}
System.out.println("Distance of Point3D: " + sum3D);
Result:
Point3D [1.0,4.0,7.0]
Point3D [3.0,6.0,9.0]
Point3D [5.0,7.0,3.0]
Distance of Point3D: 28.459444144102086 17
2. Polymorphism in Java
❑ Polymorphism is one of the four fundamental properties of OOP
programming in Java, allowing different objects to perform the same
functionality in different ways.
18
2. Polymorphism in Java
❑ There are two types of polymorphism in java, compile time polymorphism
and runtime polymorphism. We can implement polymorphism in java by
method overloading and method overriding.
19
2. Polymorphism in Java
20
2. Polymorphism in Java
❑ Type casting diagram
character
float double
21
2. Polymorphism in Java – Example
Point2D Point3D
• attributes •attributes
ox ox, y
oy oz
• constructors •constructors
o getX(), setX() Method •getX(), getY()
o getY(), setY() overriding
•setX(), setY()
• methods: •getZ(), setZ()
o show() •methods:
o distance() oshow()
o distance(double x, double y) o distance()
Method
overloading
22
2. Polymorphism in Java – Example
Student
Build a program with classes:
• id, full name, date of birth,
IT student class, Economics student gender, politics, law,...
class extends Student class • Constructors, gets, sets
• methods:
o Input information
o Show information
ABSTRACT
3. Abstraction in Java
26
3. Abstraction in Java
Purpose:
▪ Allows hiding detailed settings, only showing usage to
users, helping programmers implement complex
operations based on behaviors provided by objects.
▪ For example:
✓ Applications: Frameworks and libraries provide an abstraction
that allows us to build on top of it to develop our own
applications.
▪ In Java, the abstraction approach is based on
inheritance with:
• Abstract class
• Interface
27
3. Abstraction in Java
28
3. Abstraction in Java
❖ Abstract
➢ Abstract class does not allow instantiation of objects from
abstract class.
➢ Derived classes of abstract classes can:
✓ abstract class → No need to implement abstract methods.
✓ regular class → It is required to implement abstract methods of the class.
abstract
➢ class can be inherited → Inherit a regular class or an
abstract class.
➢ The scope of an abstract method cannot be private because
it needs to be redefined in a derived class.
➢ Since Java 7, abstract class may or may not have abstract
methods but abstract class cannot create instance even if it
does not have abstract methods.
29
3. Abstraction in Java
30
3. Abstraction in Java
Abstract Interface
Abstract classes have abstract methods Interface has only abstract methods.
(without function bodies) or non - From Java 8, it has additional default and
abstract methods (with jaw). static methods.
31
Example
❑ Interface – abstract class
To build the Paint application that allows users to perform
operations with shapes including: Point, Circle, Line, Triangle,
Rectangle, ... people build the space of shapes with the OOP
approach.
➢ Building Interface Shape with methods:
✓ Calculation: area , perimeter , distance , ..
32
Example
Interface – abstract class
1. Set <<interface>>
✓ Define Shape methods ( calculate + variable )
2. Implement the classes
✓ Point, Circle, Line, Triangle, Rectangle …
✓ attributes/constructors/getters and setters/toString
✓ area/perimeter/distance/move/rotate/zoom
EXPANDED CONCEPTS
Inner/Outer class
Concept:
➢ In Java, it is allowed to declare class /interface lie in a class /
interface people call nested class → nested class
➢ The containing class is called the outer class and the declared class
inside is called the inner class.
➢ Advantage:
❖ Group classes with the same logic in one file for management.
❖ The inner class is then considered a part of the outer class so it can access the
properties/methods of the outer class even if the properties/methods are private.
❖ Saves code in accessing outer components.
➢ Disadvantages:
❖ The inner class and its methods cannot be used in other classes.
❖ Inner class references outer class so the created object is only released when
outer object is released memory waste. Inner arrays should not be used.
37
38
Inner/Outer class
✓ Static nested class
✓ Treated as a static member of the outer class→
✓ Can be accessed through the outer class.
✓ Access only static members of the outer class .
39
Inner/Outer class
✓ Non static nested class – inner class
Considered as 2 classes when compiled however
✓ Accessed through the outer class.
✓ Can access the properties/methods of the outer class .
40
Inner/Outer class
✓ Local inside class
✓ Class inside Okay declare newspaper in one direction awake
belong to class outer.
Then the inner class cannot be called fromoutside .
41
Inner/Outer class
✓ Anonymous inside class
✓ The inner class is created during the implementation of virtual
methods of an abstract class or interface
42