KEMBAR78
OOP Ch05 Inheritance and Polymorphism | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
5 views67 pages

OOP Ch05 Inheritance and Polymorphism

OOP-Java

Uploaded by

daiphat235
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)
5 views67 pages

OOP Ch05 Inheritance and Polymorphism

OOP-Java

Uploaded by

daiphat235
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/ 67

CHAPTER 5

Inheritance and Polymorphism


Objectives
• Study concepts: superclass, subclass (Base and derived classes)
• Understand common relationships
• Functions in inheritance
• Using an “instanceof” operator
• Polymorphism
• Overloading
• Overriding
• Interface
• Abstract class
• Anonymous Classes
• Case study
Motivations
Suppose you will define classes to model circles, rectangles, and
triangles. These classes have many common features. What is
the best way to design these classes so to avoid redundancy?
Inheritance
• Object-oriented programming allows you
to define new classes from existing
classes ➔ Re-used code, save time.
• Inheritance enables you to define a
general class (i.e., a superclass) and later
extend it to more specialized classes (i.e.,
subclasses).
• A superclass is also referred to as a parent
class or a base class, and a subclass as a
child class, an extended class, or a derived
class.
• A subclass inherits accessible data fields and
methods from its superclass and may also
add new data fields and methods.
GeometricObject
-color: String The color of the object (default: white).
-filled: boolean Indicates whether the object is filled with a color (default: false).
-dateCreated: java.util.Date The date when the object was created.
+GeometricObject() Creates a GeometricObject.
+GeometricObject(color: String, Creates a GeometricObject with the specified color and filled
filled: boolean) values.
+getColor(): String Returns the color.
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date Returns the dateCreated.
GeometricObject
+toString(): String Returns a string representation of this object.
Circle

Circle Rectangle Rectangle


-radius: double -width: double
+Circle() -height: double TestCircleRectangle
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void
+printCircle(): void +getArea(): double
+getPerimeter(): double
public class SimpleGeometricObject { /** Set a new color */
private String color = "white"; public void setColor(String color) { this.color = color; }
private boolean filled;
private java.util.Date dateCreated; /** Return filled. Since filled is boolean,
/** Construct a default geometric object */ its get method is named isFilled */
public SimpleGeometricObject() { public boolean isFilled() { return filled; }
dateCreated = new java.util.Date();
} /** Set a new filled */
public void setFilled(boolean filled) { this.filled = filled; }
/** Construct a geometric object with the specified color
* and filled value */ /** Get dateCreated */
public SimpleGeometricObject(String color, boolean filled) { public java.util.Date getDateCreated() { return dateCreated; }
dateCreated = new java.util.Date();
this.color = color; /** Return a string representation of this object */
this.filled = filled; public String toString() {
} return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
/** Return color */ }
public String getColor() { return color; } }
/** Return radius */
public class CircleFromSimpleGeometricObject
public double getRadius() { return radius; }
extends SimpleGeometricObject {
/** Set a new radius */
private double radius;
public void setRadius(double radius) { this.radius = radius; }
public CircleFromSimpleGeometricObject() { }
/** Return area */
public CircleFromSimpleGeometricObject(double radius) {
public double getArea() { return radius * radius * Math.PI; }
this.radius = radius;
/** Return diameter */
}
public double getDiameter() { return 2 * radius; }
public CircleFromSimpleGeometricObject(double radius,
/** Return perimeter */
String color, boolean filled) {
public double getPerimeter() { return 2 * radius * Math.PI; }
this.radius = radius;
/* Print the circle info */
setColor(color);
public void printCircle() {
setFilled(filled);
System.out.println("The circle is created " + getDateCreated() +
}
" and the radius is " + radius);
}
}
public class RectangleFromSimpleGeometricObject /** Return width */
extends SimpleGeometricObject { public double getWidth() { return width; }
private double width; /** Set a new width */
private double height; public void setWidth(double width) { this.width = width; }
public RectangleFromSimpleGeometricObject () { } /** Return height */
public RectangleFromSimpleGeometricObject ( double width, public double getHeight() { return height; }
double height) {
/** Set a new height */
this.width = width;
public void setHeight(double height) { this.height = height; }
this.height = height; /** Return area */
}
public double getArea() { return width * height; }
public RectangleFromSimpleGeometricObject (double width,
/** Return perimeter */
double height, String color, boolean filled) {
public double getPerimeter() { return 2 * (width + height); }
this.width = width;
}
this.height = height;
setColor(color);
setFilled(filled);
}
public class TestCircleRectangle {
public static void main(String[] args) {
CircleFromSimpleGeometricObject circle =
new CircleFromSimpleGeometricObject(1);
System.out.println("A circle " + circle.toString());
System.out.println("The color is " + circle.getColor());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());

RectangleFromSimpleGeometricObject rectangle =
new RectangleFromSimpleGeometricObject(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
Access Level
Modifier Same Class Subclass (Same Package) Subclass (Different Package)
private
default
protected (but only via inheritance)
public

A subclass cannot access its superclass’s private properties


directly. It can only interact with them through inherited
public/protected methods, or if the superclass exposes
those private members via methods.
Note class SuperClass {
private int value = 42;

• Subclass does not inherit private fields protected int getValue() {


— meaning it cannot access them return value;
directly, or even override them. }
}
• But when a SubClass object is created,
the private members still exist inside class SubClass extends SuperClass
{
the object as part of the SuperClass's
public void printValue() {
state. //
• Subclass can interact with those System.out.println(value); /**
Compile error: value is private
private members only via methods */
inherited from the superclass.
System.out.println(getValue());
/** Accesses it through
inherited method */
}
}
Access Modifier Overridden

Legal private default protected public

Illegal public protected default private

The sub-class must be more opened than it’s father


Relationship among Classes
Inheritance (Is-A)

Generalization: A class extends Realization: A class implements an


another class interface.
Dependence (Uses-A)

An object of one class might use an object of another


class in the code of a method.
Association

• Classes know about each other, but


no ownership or lifecycle
management. An object might store another object in a field
• One class can access or communicate
with another.
• It’s the weakest form of relationship.

Two objects might store each other in fields


Association

A single student can associate


with multiple teachers

An Instructor has one or more


Students

We can also indicate the behavior


of an object in an association
Aggregation

• “Whole" and "part" can exist


independently.
• The whole has-a part, but the part is
shared or reusable.
• The contained class (part) can exist
without the container (whole).

One object A has or owns another object B,


and/or B is part of A
Composition

• Whole owns the parts exclusively.


• Parts cannot exist without the whole,
if the whole is destroyed, so are the
parts => Lifetime is tightly coupled.
• The container is responsible for
creating and destroying the parts.

In addition to an aggregration relationship,


the lifetimes of the objects might be identical
Has-A relationship

Relationship Real-World Example Java Behavior Lifetime Dependency


Association Book Person Method calls, data sharing
Aggregation University Student University has Students
Composition Person → Body part Person creates and owns Head

Who creates the object?


Who controls its lifetime?
Can the part exist independently?
Defining a Subclass
• In Java, the extends keyword is used to create a subclass from a
superclass.
• A class can be directly derived from only one class
• If a class does not have any superclass, then it is implicitly derived from Object
class.
• The subclass inherits all accessible fields and methods of the
superclass. You can also:
• Add new properties
• Add new methods
• Override the methods of the superclass
Superclass’s Constructor
• Constructors Are Not Inherited
• Superclass's constructors can only be invoked from the subclasses'
constructors, using the keyword super. The statement that uses the
keyword super appear first in the constructor.
• If the keyword super is not explicitly used, the superclass's no-arg
constructor is automatically invoked.
Constructor Chaining
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}

public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}

class Employee extends Person {


public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}

public Employee(String s) {
System.out.println(s);
}
}

class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
} 23
Using the Keyword super
• The keyword super refers to the superclass of the class in which
super appears. This keyword can be used in two ways:
• To call a superclass constructor
• To call a superclass method

super.method(arguments);
Quiz
What is the output of running the class C in (a)? What problem arises in
compiling the program in (b)?
Calling Superclass Methods
• Use keyword super to call superclass methods:

public void printCircle() {


System.out.println("The circle is created " +
super.getDateCreated() + " and the radius is " + radius);
}
Overriding Methods in the 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.
• Use the @Override annotation that instructs the compiler that you intend to
override a method in the superclass (it’s optional because overriding is
default in Java).
public class Circle extends GeometricObject {
// Other methods are omitted

/** Override the toString method defined in GeometricObject */


public String toString() {
return super.toString() + "\nradius is " + radius;
}
}
Note on Overriding Methods
• 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.
• 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 method that gets called is based on the reference type, not the object
type.
Hiding Method
Overriding vs. Overloading
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.p(10); a.p(10);
a.p(10.0); a.p(10.0);
} }
} }

class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }

class A extends B { class A extends B {


// This method overrides the method in B // This method overloads the method in B
public void p(double i) { public void p(int i) {
System.out.println(i); System.out.println(i);
} }
} }

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.
Polymorphism
• Polymorphism means that a variable of a supertype can refer to a
subtype object.
• With polymorphism, you can write code that doesn’t have to change
when you introduce new subclass types into the program.
Animal[] animals = new Animal[5];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Wolf();
animals[3] = new Hippo();
animals[4] = new Lion();
for (Animal animal : animals) {
animal.eat();
animal.roam();
}
Dynamic Binding
public class PolymorphismDemo {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
When the method m(Object x) is
m(new Person()); executed, the argument x’s toString
m(new Object());
}
method is invoked. x may be an
instance of GraduateStudent, Student,
public static void m(Object x) {
System.out.println(x.toString());
Person, or Object.
}
} Classes GraduateStudent, Student,
Person, and Object have their own
class GraduateStudent extends Student {
} implementation of the toString
class Student extends Person {
method. Which implementation is
public String toString() { used will be determined dynamically
return "Student"; by the Java Virtual Machine at
}
} runtime. This capability is known as
class Person extends Object {
dynamic binding.
public String toString() {
return "Person";
}
}
Dynamic Binding
Dynamic binding works as follows: Suppose an object o is an instance of
classes C1, C2, ..., Cn-1, and Cn, where C1 is a subclass of C2, C2 is a subclass of
C3, ..., and Cn-1 is a subclass of Cn. That is, Cn is the most general class, and C1
is the most specific class. In Java, Cn is the Object class. If o invokes a method
p, the JVM searches the implementation for the method p in C1, C2, ..., Cn-1
and Cn, in this order, until it is found. Once an implementation is found, the
search stops and the first-found implementation is invoked.

Cn Cn-1 ..... C2 C1

Since o is an instance of C1, o is also an


Object instance of C2, C3, …, Cn-1, and Cn
Generic Programming
public class PolymorphismDemo { Polymorphism allows methods to be used
public static void main(String[] args) {
m(new GraduateStudent()); generically for a wide range of object
m(new Student());
m(new Person());
arguments. This is known as generic
m(new Object()); programming.
}
If a method’s parameter type is a
public static void m(Object x) {
System.out.println(x.toString()); superclass (e.g., Object), you may pass an
} object to this method of any of the
}
parameter’s subclasses (e.g., Student or
class GraduateStudent extends Student {
}
String).
class Student extends Person {
When an object (e.g., a Student object or a
public String toString() { String object) is used in the method, the
return "Student";
}
particular implementation of the method
} of the object that is invoked (e.g., toString)
class Person extends Object { is determined dynamically.
public String toString() {
return "Person";
}
}
class Vet {
public void giveShot(Animal a) {
a.makeNoise();
}
}

class PetOwner {
public void start() {
Vet vet = new Vet();
Dog dog = new Dog();
Hippo hippo = new Hippo();
vet.giveShot(dog);
vet.giveShot(hippo); }
}
Casting Objects
Casting is used to convert an object of one class type to another within an
inheritance hierarchy. In the preceding section, the statement
m(new Student());

assigns the object new Student() to a parameter of the Object type. This statement
is equivalent to:

Object o = new Student(); // Implicit casting


m(o);

The statement Object o = new Student(), known as


implicit casting, is legal because an instance of
Student is automatically an instance of Object.
Casting from Superclass to Subclass
Explicit casting must be used when casting an object from a superclass
to a subclass. This type of casting may not always succeed.
Apple x = (Apple)fruit;

Orange x = (Orange)fruit;
The instanceof Operator
Use the instanceof operator to test whether an object is an instance of a class:

Object myObject = new Circle();


... // Some lines of code
/** Perform casting if myObject is an instance of
Circle */
if (myObject instanceof Circle) {
System.out.println("The circle diameter is " +
((Circle)myObject).getDiameter());
...
}

39
The equals Method
• The equals() method compares the
contents of two objects.
• The == comparison operator:
• comparing two primitive data type
values
• determining whether two objects
have the same references
The equals Method
The default implementation of the equals method in the Object class is
as follows:
public boolean equals(Object obj) {
return this == obj;
}

You’re meant to public boolean equals(Object o) {


if (o instanceof Circle) {
override equals() when return radius == ((Circle)o).radius;
you want to define }
custom comparison else
return false;
logic }
Interface & Abstract Class
Interfaces
• In Java, interfaces define contracts for what a class must
do, but not how it does it.
• An interface is a reference type, similar to a class, that can
contain only:
• abstract methods: Methods without a body (must be
implemented)
• default methods: Method with a default body (optional to
override)
• static methods: Method with body, accessed via
InterfaceName.method()
• private methods: (Java 9+) Used internally by other methods
in the interface
• Constants: Variables must be public static final by default
• Interface can be used as a data type for a variable, as the
result of casting, and so on.
• Interfaces cannot be instantiated because they have no-
body methods.
• Interfaces can only be implemented by classes
or extended by other interfaces.
Define an Interface
To distinguish an interface from a class, Java uses the following syntax
to define an interface:
public interface InterfaceName {
constant declarations;
abstract method signatures;
}

Example:
public interface Edible {
/** Describe how to eat */
public abstract String howToEat();
}

44 44
Omitting Modifiers in Interfaces
All data fields are public final static and all methods are public abstract in an
interface. For this reason, these modifiers can be omitted, as shown below:

public interface T1 { public interface T1 {


public static final int K = 1; Equivalent int K = 1;

public abstract void p(); void p();


} }

A constant defined in an interface can be accessed using syntax


InterfaceName.CONSTANT_NAME (e.g., T1.K).
Why and when to use interfaces?
• Objects define their interaction with the outside world through
the methods that they expose
• Java does not support "multiple inheritance" (a class can only
inherit from one superclass). However, it can be achieved with
interfaces, because the class can implement multiple interfaces
Interfaces example
Interfaces example

m3(), m4() in A cannot


implement m3(), m4()
in InterfaceDemo,
attempting to assign
weaker access
privileges, were public

Default methods of an interface must be overridden as public methods in


concrete classes.
Example: how to create an interface
Example:implement an interface
Output:
set lock in the default method
on TV
off TV
shut down after 10000 seconds
TV remote's price:10
TV Remote has: 20buttons
Example: multiple interfaces

Output:
set lock in the default method
on TV
off TV
shut down after 10000 seconds
TV remote's price:10
TV Remote has: 20buttons
increase volumn
Example: how to extend interfaces

Output:
on AC
display Korean
set lock in the
default method
Standard API Interfaces
Interface Purpose
Comparable<T> Natural ordering of objects (compareTo)
Comparator<T> Custom sorting logic
Iterable<T> Used in for-each loops
Collection<E> Root of Java Collection Framework
Serializable Marks a class as serializable
Closeable Used in try-with-resources
AutoCloseable Like Closeable, more generic
Callable<V> Like Runnable, but returns value
Abstract Classes
• Abstract method: behaviors a class is required to perform without
having to provide an explicit implementation.
• Abstract class: A class that contains abstract methods must be
abstract.
• An abstract class can also declare implemented methods.
• An abstract class can contain no abstract methods.
• Cannot create instances of the abstract class using the new operator.
• If a subclass of an abstract superclass does not implement all the abstract
methods, the subclass must be defined abstract.
• Syntax to define a abstract class
public abstract class className{ ... }
Abstract Classes Example

Modified
Abstract Classes Example

This class have no abstract method but it is declared as an


abstract class. So, we can not initiate an object of this class.
Abstract Classes…

Error. Why?
Implementing Abstract Methods
• Derive a class from an abstract superclass, the subclass will inherit all
of the superclass’s features, all of abstract methods included.
• To replace an inherited abstract method with a concrete version, the
subclass need merely override it.
• Abstract classes cannot be instantiated
Feature Abstract Class Interface
For sharing common code among related For defining a contract (what
Use case
classes should be done)
“is-a” relationship with some common
Represents “can-do” relationship (capability)
implementation
Keyword abstract class interface
Inheritance type Single inheritance Multiple inheritance
Constructors Allowed Not allowed
Field types Any type (instance/static, any access) Only public static final (constants)
Only public (for non-private
Method access modifiers Any (public, protected, etc.)
methods)
Concrete methods allowed? Yes Yes (with default, static)
Private methods allowed? Yes Yes (since Java 9)
Object state (fields) Can have instance variables No instance variables
Usage keyword in subclass extends implements
Whether to use an interface or a class?
• A strong is-a relationship that clearly describes a parent-child
relationship should be modeled using classes.
• Ex. A staff member is a person.

• A weak is-a relationship, also known as an is-kind-of relationship,


indicates that an object possesses a certain property. A weak is-a
relationship can be modeled using interfaces.
• Ex. all strings are comparable, so the String class implements the Comparable
interface.

• You can also use interfaces to circumvent single inheritance restriction if


multiple inheritance is desired. In the case of multiple inheritance, you
have to design one as a superclass, and others as interface.
Anonymous Classes
• An anonymous class is a class without a name that is declared and
instantiated all at once, usually to override methods of an interface
or abstract class, or for short-lived use.
• Why are they used?
• Enable you to make your code more concise.
• Enable you to declare and instantiate a class at the same time.
• They are like local classes except that they do not have a name.
• Use them if you need to use a local class only once.
Anonymous Class

Anonymous class.

Class name is given by the


compiler:
ContainerClass$Number
Anonymous Class…
Concrete methods but they can
not be used because the class is
declared as abstract one.

Anonymous class is a technique is commonly used to support


programmer when only some methods are overridden only
especially in event programming.
Case study
• A antique shop that sells antique items, namely vases, statues, and
paintings. The owner can add item to inventory. The shop will keep
items in the list. The owner can add a new item to it, he search also
the item,….
=> For now, we want to manage the list of objects such as vases,
statues, paintings in an array or a list.
Summary
• Object-oriented languages implement reusability of coding structure
through inheritance
• A derived class does not by default inherit the constructor of a super
class
• Constructors in an inheritance hierarchy execute in order from the
super class to the derived class
• Using the instanceof keyword if we need to check the type of the
reference variable.
• Check the type of the reference variable before casting it explicitly.
Summary
• Polymorphism is a concept of object-oriented programming
• Polymorphism is the ability of an object to take on many forms
• Overloading and overriding are a technology to implement
polymorphism feature.
• In OOP occurs when a parent class/ interface reference is used to
refer to a child class object

You might also like