Inheritance
Inheritance
By Dr. AJK
© Templateswise.com
Objectives
© 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;
© 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:
© 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 {
© 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