Computer Programming (2)
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:
[Link]
• 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
Cat Dog Fox
• Data and behavior associated with super-classes are accessible
to those subclasses
7
Super classes and Subclasses
Land Vehicle
Bus Car Truck
Toyota
Yaris Corolla Camry
8
Super classes and Subclasses
• Terms used in Inheritance
• Class: A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
Ø The derived class inherits:
Ø all the public methods.
Ø all the public and private instance variables.
Ø all the public and private static variables from the base class.
Ø The derived class can add more instance variables, static variables, and/or methods.
Reference: [Link]
Super classes and Subclasses
• Terms used in Inheritance
• Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
• Reusability: is a mechanism which facilitates you to reuse the fields and
methods of the existing class when you create a new class. You can use the
same fields and methods already defined in the previous class.
Reference: [Link]
Super classes and Subclasses
• The extends clause in a class declaration establishes an inheritance
relationship between two classes. It has the following syntax:
class DerivedClass extends BaseClass
{ Base Class
// body of the class
} Derived Class
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
• Types of inheritance in java
Reference: [Link]
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
• University Community Member Hierarchy
• protected Members
protected Members
• protected access offers:
• An intermediate level of access between public and private.
• A superclass’s protected members can be accessed:
• by members of that superclass
• by members of its subclasses
• by members of other classes in the same package
• Subclass access superclass member
• Keyword super and a dot (.)
protected Members
Reference: [Link]
• Relationship Between Super classes and Subclasses
Relationship Between Super classes and Subclasses
• Example:
class Employee{
float salary=40000;
}
class Programmer extends Employee{ Output
int bonus=10000;
Programmer salary is:40000.0
public static void main(String args[]){ Bonus of programmer is:10000
Programmer p=new Programmer();
[Link]("Programmer salary is:"+[Link]);
[Link]("Bonus of Programmer is:"+[Link]);
}
}
Reference: [Link]
Relationship Between Super classes and Subclasses
• Single Inheritance Example
class Animal{
void eat(){
[Link]("eating...");
}
}
class Dog extends Animal{ Output
void bark(){
[Link]("barking..."); barking...
} eating...
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
[Link]();
}
}
Reference: [Link]
Relationship Between Super classes and Subclasses
• Multilevel Inheritance Example
class Animal{
void eat(){ [Link]("eating..."); }
}
class Dog extends Animal{
void bark(){ [Link]("barking..."); }
} Output
class BabyDog extends Dog{
void weep(){ [Link]("weeping..."); } weeping...
} barking...
class TestInheritance2{ eating...
public static void main(String args[]){
BabyDog d=new BabyDog();
[Link]();
[Link]();
[Link]();
}
}
Reference: [Link]
Relationship Between Super classes and Subclasses
• Hierarchical Inheritance Example
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void bark(){[Link]("barking...");}
} Output
class Cat extends Animal{
void meow(){[Link]("meowing...");} meowing...
} eating...
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
[Link]();
[Link]();
//[Link]();//[Link]
}
}
Reference: [Link]
Relationship Between Super classes and Subclasses
• The final Keyword
• If you don't want other classes to inherit from a class, use the final keyword:
final class Vehicle {
...
} Output
class Car extends Vehicle { [Link][Link] error: cannot inherit from final Vehicle
... class Main extends Vehicle {
} ^
1 error)
Reference: [Link]
Relationship Between Super classes and Subclasses
• Example for Super Keyword in Java:
class Superclass {
int i =20;
void display() {
[Link](“Superclass display method”);
}
}
class Subclass extends Superclass { Output
int i = 100;
void display() { Superclass display method
[Link](); Subclass display method
[Link](“Subclass display method”);
[Link](“ i value =”+i); i value =100
[Link](“superclass i value =”+super.i); superclass i value =20
}
}
class SuperUse {
public static void main(String args[]) {
Subclass obj = new Subclass();
[Link]();
}
}
Reference: [Link]
Thank You