0% found this document useful (0 votes)
4 views21 pages

Inheritance

The document provides an overview of inheritance in object-oriented programming, explaining its objectives, types, and implementation. It details the concepts of superclass and subclass, the use of the 'super' keyword, and the IS-A relationship. Additionally, it includes code examples to illustrate these concepts and concludes with an assignment for practical application.

Uploaded by

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

Inheritance

The document provides an overview of inheritance in object-oriented programming, explaining its objectives, types, and implementation. It details the concepts of superclass and subclass, the use of the 'super' keyword, and the IS-A relationship. Additionally, it includes code examples to illustrate these concepts and concludes with an assignment for practical application.

Uploaded by

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

Inheritance

By Dr. AJK

© Templateswise.com
Objectives

The objectives are:


• To Understand Inheritance
• To know the Types of inheritance
• Implement of Inheritance
• Understand Super and this
keyword
• Understand IS-A relationship
Introduction to Inheritance
• Inheritance is a process where one class acquires the
properties (methods and attributes) of another.
• With the use of inheritance, the information is made
manageable in a hierarchical order.
• The class which inherits the properties of other is
known as subclass (derived class, child class).
• The class whose properties are inherited is known as
superclass (base class, parent class).
Need for Inheritance
• Code reusability: The basic need of an inheritance is to reuse the
features.
• Extensibility: The inheritance helps to extend the functionalities of a
class.
• Implantation of Method Overriding: Inheritance is required to
achieve overriding of methods.
• Achieving Abstraction: Another concept of OOPs that is abstraction
also needs inheritance.

© Templateswise.com
Types of
Inheritance

© Templateswise.com
Types of
Inheritance

© Templateswise.com
Basic Syntax to Implement Inheritance
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}

© Templateswise.com
class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given
numbers:"+z);
}
}
© Templateswise.com
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
} }
© Templateswise.com
Explanation
• There are two classes in the example: Calculation and
My_Calculation.
• Calculation: field – z, methods – addition, substraction.
• My Calculation: fields – a, b, methods – multiplication,
inherited class – Calculation, object – demo.

© Templateswise.com
The super keyword
• The “super” keyword is similar to “this” keyword.
• It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
• It is used to invoke the superclass constructor from subclass.
• If a class is inheriting the properties of another class.
• And if the members of the superclass have the names same as the
sub class, to differentiate these variables we use super keyword as
shown below.
• super.variable
• super.method();
© Templateswise.com
class Super_class {
int num = 20;

// display method of superclass


public void display() {
System.out.println("This is the display method of superclass");
}
}
public class Sub_class extends Super_class {
int num = 10;

© Templateswise.com
// display method of sub class
public void display() {
System.out.println("This is the display method of subclass");
}
public void my_method() {
Sub_class sub = new Sub_class();
sub.display();
super.display();
System.out.println("value of the variable named num in sub class:"+
sub.num);
System.out.println("value of the variable named num in super class:"+
super.num);
}
© Templateswise.com
public static void main(String args[]) {
Sub_class obj = new Sub_class();
obj.my_method();
}
}
Output:

This is the display method of subclass


This is the display method of superclass
value of the variable named num in sub class:10
value of the variable named num in super class:20

© Templateswise.com
Invoking Superclass Constructor
• If a class is inheriting the properties of another class, the subclass
automatically acquires the default constructor of the superclass.
• But if you want to call a parameterized constructor of the
superclass, you need to use the super keyword “super(values);”.
• class Superclass {
• int age;
• Superclass(int age) {
• this.age = age;
• }

© Templateswise.com
public void getAge() {
System.out.println("The value of the variable named age in super class
is: " +age);
}
}
public class Subclass extends Superclass {
Subclass(int age) {
super(age);
}
public static void main(String args[]) {
Subclass s = new Subclass(24);
s.getAge();
} }
© Templateswise.com
IS-A Relationship
• IS-A is a way of saying: This object is a type of that object.
• Let us see how the extends keyword is used to achieve inheritance.
class Animal {
}
class Mammal extends Animal {
}
class Reptile extends Animal {
}

© Templateswise.com
public class Dog extends Mammal {

public static void main(String args[]) {


Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}

© Templateswise.com
Output
• Output:
• true
• true
• true

© Templateswise.com
Assignment
• Using any example animals or students, show an example
of the different types of inheritance. To submit, upload the
code to your github and paste the link in the excel sheet
that the link will be sent to your email. Note that you are
enter your reg no, mat no, name and the link in the excel to
have a valid submission.

© Templateswise.com
THANK YOU
FOR
LISTENING

You might also like