Chapter 3
Chapter 3
Chapter 3
Inheritance & Polymorphism
1. Super classes and Subclasses
2. protected Members
3. Relationship Between Super classes and Subclasses
Contents
Recommended Reading
1. Chapter 8 - (Java The Complete Reference, Eleventh Edition)
2. Java Inheritance:
https://www.tutorialspoint.com/java/java_inheritance.htm
• Super classes and Subclasses
Super classes and Subclasses
• Inheritance
• It is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
• Using this technique, further classes can be created from existing ones; those
classes are said to inherit the methods and instance variables of the class they
inherited
• Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
• Features:
• Software reusability
• Create new class from existing class
• Absorb existing class’s data and behaviors
• Enhance with new capabilities
Super classes and Subclasses
Animal
7
Super classes and Subclasses
Land Vehicle
Toyota
8
Super classes and Subclasses
Ø The derived class can add more instance variables, static variables, and/or methods.
Reference: https://www.javatpoint.com/inheritance-in-java
Super classes and Subclasses
Reference: https://www.javatpoint.com/inheritance-in-java
Super classes and Subclasses
• Class hierarchy
• Direct superclass
• Inherited explicitly (one level up hierarchy)
• Indirect superclass
• Inherited two or more levels up hierarchy
• Single inheritance
• Inherits from one superclass
• Multiple inheritance
• Inherits from multiple superclasses
• Java does not support multiple inheritance
Super classes and Subclasses
Reference: https://www.javatpoint.com/inheritance-in-java
Super classes and Subclasses
• Examples:
Super classes and Subclasses
• Inheritance hierarchy
• Inheritance relationships: tree-like hierarchy structure
• Each class becomes
• superclass
• Supply data/behaviors to other classes
OR
• subclass
• Inherit data/behaviors from other classes
Super classes and Subclasses
Reference: https://www.geeksforgeeks.org/access-modifiers-java/
• Relationship Between Super classes and Subclasses
Relationship Between Super classes and Subclasses
• Example:
class Employee{
float salary=40000;
}
Reference: https://www.javatpoint.com/inheritance-in-java
Relationship Between Super classes and Subclasses
class Car extends Vehicle { Main.java:9: error: cannot inherit from final Vehicle
... class Main extends Vehicle {
} ^
1 error)
Reference: https://www.w3schools.com/java/java_inheritance.asp
Relationship Between Super classes and Subclasses
• Example for Super Keyword in Java:
class Superclass {
int i =20;
void display() {
System.out.println(“Superclass display method”);
}
}
class Subclass extends Superclass { Output
int i = 100;
void display() { Superclass display method
super.display(); Subclass display method
System.out.println(“Subclass display method”);
System.out.println(“ i value =”+i); i value =100
System.out.println(“superclass i value =”+super.i); superclass i value =20
}
}
class SuperUse {
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.display();
}
}
Reference: https://www.mygreatlearning.com/blog/inheritance-in-java/
Thank You