0% found this document useful (0 votes)
16 views29 pages

Pr2 Lec5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views29 pages

Pr2 Lec5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

PR2 - Programming 2

Lecture 5
Inheritance and Data Abstraction
Outline
• Data abstraction
• Inheritance
References
What is data abstraction?

• Simply put, data abstraction is the modeling of real objects using


programming language.
• It allows us to extend the programming language with new data
types.
• In a given context, preserving relevant information and discarding
irrelevant information.

{
name: “Car”,
model: “M1”,
color: “red”,
power: 450
}
Features of data abstraction

• Representation (rep): the equivalent of real object properties in


programming language.
• E.g. A String is equivalent to a name, an int is equivalent to a
person’s age…
• Abstraction function: the mapping function between real object
and virtual object’s instance variables
• Rep invariant: a statement of conditions that all legitimate
objects satisfy
• Not all objects of the class are legitimate representations of abstract
objects
• E.g. {name: “Nam”, age: -1} is not a valid person object
Specifying the abstract function

• Abstract function is the mapping


between real object and virtual object
• It is implemented in the toString()
method
Inheritance

• What is inheritance?
• Why do we need inheritance?

Spring 2024 61FIT3PR2 – Programming 2 7


Inheritance

• Imagine you need to code an app to control an organization. You


have positions: Programmer and Manager. Both of these positions
have a common set of properties, including name, address, phone
number.

Programmer class Manager class

Public class Programmer{ Public class Manager{


private String address; private String address;
private String name; private String name;
private String phone; private String phone;
} }

Spring 2024 61FIT3PR2 – Programming 2 8


Inheritance

• These positions also have different properties. A programmer may be


concerned with project programming languages, whereas a manager
may be concerned with project status reports.

Programmer class Manager class

Public class Programmer{ Public class Manager{


private String address; private String address;
private String name; private String name;
private String phone; private String phone;
private String[] languages; void reportProjectStatus(){}
void writeCode(){} }
}

Spring 2024 61FIT3PR2 – Programming 2 9


Problem with the design

Spring 2024 61FIT3PR2 – Programming 2 10


Inheritance

• Pull out the common properties into a new position (a new class,
called parent class or superclass).

Programmer class Manager class Employee class

Public class Programmer Public class Manager extends Public class Employee{
extends Employee{ Employee{ private String address;
private String[] languages; void reportProjectStatus(){} private String name;
void writeCode(){} } private String phone;}
}

Spring 2024 61FIT3PR2 – Programming 2 11


Inheritance
Benefits

Smaller class definition

Ease of modification to common properties and behavior

Extensibility

Enable to reuse code

Spring 2024 61FIT3PR2 – Programming 2 12


Inheritance
• In OOP, classes are organized hierarchically to prevent duplication
and reduce redundancy.
• Classes in Java exist within a hierarchical system, known as an
inheritance tree.
• The top-level class is called the superclass, while the lower-level
classes are called subclasses.
• In Java, a class can only have a single superclass (single inheritance).

Spring 2024 61FIT3PR2 – Programming 2 13


Subclasses and Superclasses
• Subclasses inherit variables and
methods from higher-level
superclasses.
• Subclasses are also known as derived,
child, or extended classes.
• Superclasses are referred to as base or
parent classes.

Spring 2024 61FIT3PR2 – Programming 2 14


Subclasses and Superclasses
• Placing common variables and methods in superclasses minimizes
redundancy.
• Specialized variables and methods remain in subclasses.
• This approach enhances code organization and maintenance.
• Redundancy is reduced across subclasses, promoting code reuse.
• A subclass can inherit variables and methods from its superclasses,
including immediate parent and ancestors.

Spring 2024 61FIT3PR2 – Programming 2 15


Subclasses and Superclasses
• The subclass is allowed to possess the properties (fields and
methods) of the parent class.
• The subclass is permitted to possess the public or protected properties of the
parent class.
• The subclass is also allowed to possess the {default} properties of the parent
class if both the subclass and the parent class are defined within the same
package.
• The subclass cannot access the private members of the parent class.
• The subclass does not inherit the constructors of the parent class.

Spring 2024 61FIT3PR2 – Programming 2 16


Example:

class Goalkeeper extends SoccerPlayer {......}


class MyApplet extends java.applet.Applet {.....}
class Cylinder extends Circle {......}

Spring 2024 61FIT3PR2 – Programming 2 17


Keyword "super"

• Accessing the members of the


parent class is done using the super
keyword.
• super can be used to call the
constructor of the parent class.

Spring 2024 61FIT3PR2 – Programming 2 18


EG. 1: Deriving a Subclass from a Superclass
• Reuse of the Circle class is
emphasized
• Cylinder inherits member
variables (radius, color) and
methods (getRadius(),
getArea(), etc.) from Circle
• Cylinder introduces its own
variable (height) and methods
(getHeight(), getVolume())
• Constructors for Cylinder are
defined

Spring 2024 61FIT3PR2 – Programming 2 19


Method Overriding
• Overriding occurs when both the subclass and the superclass have methods
with the same syntax.

• Both the Parent and Child classes have a


method() with the same syntax, so the method()
in Child will override the method() in Parent.

Spring 2024 61FIT3PR2 – Programming 2 20


Method Overriding

Even though the object has the type Parent, when


o.method() is called, the method() of the Child
class will execute because it overrides the
method() of the Parent class.

Spring 2024 61FIT3PR2 – Programming 2 21


Method Overriding

• Subclass overriding a method of the superclass


will hide the superclass method.
• The purpose of overriding is to modify the method
of the superclass in the subclass.
• Use the keyword 'super' to access the overridden
method of the superclass.
• The access specifier of the subclass method must
be at least as public as the access specifier of
the superclass method.

Spring 2024 61FIT3PR2 – Programming 2 22


For example for Method Overriding

The inherited method getArea() in a Cylinder


object computes the base area of the cylinder.
Suppose that we decide to override the getArea()
to compute the surface area of the cylinder in the
subclass Cylinder.

Spring 2024 61FIT3PR2 – Programming 2 23


For example for Method Overriding
public class Cylinder extends Circle {
......
// Override the getArea() method inherited from superclass Circle
@Override
public double getArea() {
return 2*Math.PI*getRadius()*height + 2*super.getArea();
}
// Need to change the getVolume() as well
public double getVolume() {
return super.getArea()*height; // use superclass' getArea()
}
// Override the inherited toString()
@Override
public String toString() {
return "Cylinder[" + super.toString() + ",height=" + height + "]";
}
}

Spring 2024 61FIT3PR2 – Programming 2 24


For example for Method Overriding

• If getArea() is called from a Circle object, it computes


the area of the circle.
• If getArea() is called from a Cylinder object, it
computes the surface area of the cylinder using the
overridden implementation.
• Note that you have to use public accessor method
getRadius() to retrieve the radius of the Circle,
because radius is declared private and therefore not
accessible to other classes, including the subclass
Cylinder.

Spring 2024 61FIT3PR2 – Programming 2 25


For example for Method Overriding

• But if you override the getArea() in the Cylinder, the


getVolume() (= getArea()*height) no longer works. It
is because the overridden getArea() will be used in
Cylinder, which does not compute the base area.
• You can fix this problem by using super. getArea()to
use the superclass' version of getArea().
• Note that super. getArea()can only be issued from
the subclass definition, but no from an instance created,
e.g. c1.super. getArea(), as it break the information
hiding and encapsulation principle.

Spring 2024 61FIT3PR2 – Programming 2 26


Single Inheritance

• Java does not support multiple inheritance (C++ does).


Multiple inheritance permits a subclass to have more
than one direct superclasses.
• This has a serious drawback if the superclasses have
conflicting implementation for the same method.
• In Java, each subclass can have one and only one
direct superclass, i.e., single inheritance. On the other
hand, a superclass can have many subclasses.

Spring 2024 61FIT3PR2 – Programming 2 27


EG. 2: The Point2D and Point3D Classes

• Implement Point2D.java,
Point3D.java
and
TestPoint2DPoint3D.java

Spring 2024 61FIT3PR2 – Programming 2 28


Summary

• Inheritance
• Subclasses and Superclass
• Method Overriding
• Single Inheritance

Spring 2024 61FIT3PR2 – Programming 2 29

You might also like