0% found this document useful (0 votes)
7 views27 pages

Chapter 3

The document discusses inheritance in object-oriented programming with Java. It defines key concepts like superclass, subclass, inheritance, polymorphism and explores examples of single, multilevel and hierarchical inheritance with classes extending behaviors and properties from superclasses.

Uploaded by

alqrshyhsn82
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)
7 views27 pages

Chapter 3

The document discusses inheritance in object-oriented programming with Java. It defines key concepts like superclass, subclass, inheritance, polymorphism and explores examples of single, multilevel and hierarchical inheritance with classes extending behaviors and properties from superclasses.

Uploaded by

alqrshyhsn82
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/ 27

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:
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

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: https://www.javatpoint.com/inheritance-in-java
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: https://www.javatpoint.com/inheritance-in-java
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: 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

• 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: 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;
}

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();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

Reference: https://www.javatpoint.com/inheritance-in-java
Relationship Between Super classes and Subclasses

• Single Inheritance Example


class Animal{
void eat(){
System.out.println("eating...");
}
}
class Dog extends Animal{ Output
void bark(){
System.out.println("barking..."); barking...
} eating...
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
Reference: https://www.javatpoint.com/inheritance-in-java
Relationship Between Super classes and Subclasses
• Multilevel Inheritance Example
class Animal{
void eat(){ System.out.println("eating..."); }
}
class Dog extends Animal{
void bark(){ System.out.println("barking..."); }
} Output
class BabyDog extends Dog{
void weep(){ System.out.println("weeping..."); } weeping...
} barking...
class TestInheritance2{ eating...
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Reference: https://www.javatpoint.com/inheritance-in-java
Relationship Between Super classes and Subclasses
• Hierarchical Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
} Output
class Cat extends Animal{
void meow(){System.out.println("meowing...");} meowing...
} eating...
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
Reference: https://www.javatpoint.com/inheritance-in-java
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 { 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

You might also like