البرمجة غرضية التوجه
OOP
References
- Y. Daniel Liang - Introduction to Java Programming and Data Structures, Comprehensive Version-
Pearson (2019)
Inheritance
• Introduction
– Inheritance can be defined as the procedure or mechanism of acquiring all the properties and
behavior of one class to another
• i.e. acquiring the properties and behavior of child class from the parent class.
– This concept was built in order to achieve the advantage of creating a new class that gets
built upon an already existing class(es).
– It is mainly used for code reusability within a Java program.
– The class that gets inherited taking the properties of another class is the subclass or derived
class or child class.
– Again, the class whose properties get inherited is the superclass or base class or parent class.
– The keyword extends is used to inherit the properties of the base class to derived class.
– The structure of using this keyword looks something like this:
• class base {
.....
.....
}
class derive extends base {
.....
.....
}
1
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
• Visibility Modifiers
– Using to specify the visibility of a class and its members.
– The public visibility modifier can be used for classes, methods, and data fields to denote
they can be accessed from any other classes.
– The default visibility modifier is used for classes, methods, and data fields to denote they
can be accessible by any class in the same package.
• This is known as package-private or package-access.
– The private modifier makes methods and data fields accessible only from within its own
class.
• An object can access its private members if it is defined in its own class.
2
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
• Example:
– public class Circle {
private double radius = 1;
private static int numberOfObjects = 0;
public Circle() { numberOfObjects++; }
public Circle(double newRadius) {
radius = newRadius;
numberOfObjects++; }
public double getRadius() { return radius; }
public void setRadius(double newRadius) { radius = (newRadius >= 0) ?
newRadius : 0; }
public static int getNumberOfObjects() {return numberOfObjects; }
public double getArea() { return radius * radius * [Link]; }
}
– The protected modifier makes methods and data fields accessible only from within its own
class and a subclass.
– specify how classes and class members are accessed.
• The visibility of these modifiers increases in this order:
3
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
– Example:
4
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
– Example: [Link]
• public class GeometricObject {
private String color = "white";
private boolean filled;
private [Link] dateCreated;
public GeometricObject() {
dateCreated = new [Link](); }
public GeometricObject(String color, boolean filled) {
dateCreated = new [Link]();
[Link] = color;
[Link] = filled; }
public String getColor() { return color; }
public void setColor(String color) {[Link] = color; }
public boolean isFilled() { return filled; }
public void setFilled(boolean filled) { [Link] = filled; }
public [Link] getDateCreated() { return dateCreated; }
public String toString() { return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled; }
}
– Example: [Link]
5
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
• public class Circle extends GeometricObject {
private double radius;
public Circle() { }
public Circle(double radius) {
[Link] = radius; }
public Circle(double radius, String color, boolean filled) {
[Link] = radius; setColor(color); setFilled(filled); }
public double getRadius() { return radius; }
public void setRadius(double radius) { [Link] = radius; }
public double getArea() { return radius * radius * [Link]; }
public double getDiameter() { return 2 * radius; }
public double getPerimeter() { return 2 * radius * [Link]; }
public void printCircle() { [Link]("The circle is created " +
getDateCreated() + " and the radius is " + radius); }
}
• Is it possible to write:
• public Circle(double radius, String color, boolean filled) {
[Link] = radius;
[Link] = color; // Illegal
[Link] = filled; // Illegal
}
– Example: [Link]
• public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() { }
public Rectangle(double width, double height) { [Link] = width; [Link] =
height; }
public Rectangle( double width, double height, String color, boolean filled) {
[Link] = width; [Link] = height; setColor(color); setFilled(filled); }
public double getWidth() { return width; }
public void setWidth(double width) { [Link] = width; }
public double getHeight() { return height; }
public void setHeight(double height) { [Link] = height; }
public double getArea() { return width * height; }
public double getPerimeter() { return 2 * (width + height); }
}
– Example: [Link]
• public class TestCircleRectangle {
public static void main(String[] args) {
Circle circle = new Circle(1);
[Link]("A circle " + [Link]());
[Link]("The color is " + [Link]());
[Link]("The radius is " + [Link]());
[Link]("The area is " + [Link]());
[Link]("The diameter is " + [Link]());
Rectangle rectangle = new Rectangle(2, 4);
[Link]("\nA rectangle " + [Link]());
[Link]("The area is " + [Link]());
[Link]("The perimeter is " + [Link]()); }
}
• Using the super Keyword:
6
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
– The keyword super refers to the superclass
– It can be used to invoke the superclass’s methods and constructors.
– Example:
• public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); }
public Faculty() { [Link]("(4) Performs Faculty's tasks"); }
}
class Employee extends Person {
public Employee() {
this("(2) Invokes Employee's overloaded constructor");
[Link]("(3) Performs Employee's tasks "); }
public Employee(String s) { [Link](s); }
}
class Person {
public Person() { [Link]("(1) Performs Person's tasks"); }
}
– Example:
• What is the output of running the class C in (a)? What problem arises in compiling
the program in (b) and how correct it?
• Overriding Methods
7
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
– To override a method, the method must be defined in the subclass using the same signature
as in its superclass.
– A subclass inherits methods from a superclass.
• Sometimes, it is necessary for the subclass to modify the implementation of a method
defined in the superclass.
• This is referred to as method overriding.
– How can subclass access an overriding method in superclass?
• By using the keyword super
– The overriding method must have the same signature as the overridden method and same or
compatible return type.
• Compatible means that the overriding method’s return type is a subtype of the
overridden method’s return type.
– An instance method can be overridden only if it is accessible.
• Thus, a private method cannot be overridden, because it is not accessible outside its
own class. If a method defined in a subclass is private in its superclass, the two
methods are completely unrelated.
– Like an instance method, a static method can be inherited.
• However, a static method cannot be overridden.
• If a static method defined in the superclass is redefined in a subclass, the method
defined in the superclass is hidden.
• The hidden static methods can be invoked using the syntax
[Link]
– Example of static methods:
public class GrandFatherClass {
public static void m1(){[Link]("This is m1 from the grandfather");
}
}//End of class GrandFatherClass
//------------------------------------------------------------------
public class FatherClass extends GrandFatherClass {
public static void m1(){
[Link]("This is m1 from the father");
GrandFatherClass.m1();
//super.m1();//would result in compile time error: supper cannot be used
//in a static context
}
public void m2(){ super.m1(); } //ok BUT not preferred!
} //End of class FatherClass
//-------------------------------------------------------------------
public class SonClass extends FatherClass {
public static void m1(){
[Link]("This is m1 from the son");
GrandFatherClass.m1();
FatherClass.m1();
}
public void m3(){ super.m1();//ok BUT not preferred!
}
} //End of class SonClass
– Preventing a Method from Being Overridden:
• The final modifier will prevent the overriding of a superclass method in a subclass.
– public final void message()
• If a subclass attempts to override a final method, the compiler generates an error.
8
1st Season 2023/2024 Dr.-Eng Samer Sulaiman
• This ensures that a particular superclass method is used by subclasses rather than a
modified version of it.
– Use the final modifier to indicate that a class is final and cannot be a parent class.
• The Math, String, StringBuilder, and StringBuffer classes are also final classes.
• For example, the following class is final and cannot be extended:
public final class C {
// Data fields, constructors, and methods omitted
}
• It would be a compile error, see example TestExtendingFinalClasses
– You also can define a method to be final; a final method cannot be overridden by its
subclasses.
• For example, the following method is final and cannot be overridden:
public class Test {
// Data fields, constructors, and methods omitted
public final void m() {
// Do something
}
}
• Overriding vs. Overloading
– Overloading means to define multiple methods with the same name but different signatures.
• Overriding means to provide a new implementation for a method in the subclass.
– Example:
9
1st Season 2023/2024 Dr.-Eng Samer Sulaiman