INHERITANCE
Mrs.G.Chandraprabha,
Assistant Professor of IT
V.V.Vanniaperumal College for Women
Virudhunagar
 Inheritance can be defined as the process where one class acquires
the properties (methods and fields) 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) and the class whose properties
are inherited is known as superclass (base class, parent class).
 Extends Keyword
 extends is the keyword used to inherit the properties of a class.
Following is the syntax of extends keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super
{
.....
.....
}
• Sample Code
• Following is an example demonstrating Java inheritance. In this
example, you can observe two classes namely Calculation and
My_Calculation.
• Using extends keyword, the My_Calculation inherits the methods
addition() and Subtraction() of Calculation class.
• Copy and paste the following
EXAMPLE :-
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);
}
}
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);
}
}
Compile and execute the above code as shown below.
• javac My_Calculation.java
• java My_Calculation
After executing the program, it will produce the following result −
Output:-
• The sum of the given numbers:30
• The difference between the given numbers:10
• The product of the given numbers:200
• In the given program, when an object to My_Calculation
class is created, a copy of the contents of the superclass is
made within it. That is why, using the object of the subclass
you can access the members of a superclass.
Multiplication()
Int z
addition()
Subtraction()
Copy of calculation object
Object of MY-
CALCULATION
CLASS
• The Superclass reference variable can hold the subclass object, but
using that variable you can access only the members of the
superclass, so to access the members of both classes it is
recommended to always create reference variable to the subclass.
• If you consider the above program, you can instantiate the class as
given below. But using the superclass reference variable ( cal in
this case) you cannot call the method multiplication(), which
belongs to the subclass My_Calculation.
Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
Note − A subclass inherits all the members (fields, methods, and
nested classes) from its superclass. Constructors are not
members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
The super keyword.
The super keyword is similar to this keyword.
Following are the scenarios where the super keyword is used.
● 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.
Differentiating the Members
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();
Sample Code
This section provides you a program that demonstrates the
usage of the super keyword.
• In the given program, you have two classes namely
Sub_class and Super_class, both have a method named
display() with different implementations, and a variable
named num with different values. We are invoking
display() method of both classes and printing the
value of the variable num of both classes. Here you can
observe that we have used super keyword to
differentiate the members of superclass from subclass.
• Copy and paste the program in a file with name
Sub_class.java.
Example
Live Demo
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;
// display method of sub class
public void display() {
System.out.println("This is the display method of subclass");
}
public void my_method() {
// Instantiating subclass
Sub_class sub = new Sub_class();
// Invoking the display() method of sub class
sub.display();
// Invoking the display() method of superclass
super.display();
// printing the value of variable num of subclass
System.out.println("value of the variable named num in sub class:"+ sub.num);
// printing the value of variable num of superclass
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]) {
Sub_class obj = new Sub_class();
obj.my_method();
}
}
Compile and execute the above code using the following
syntax.
javac Super_Demo
java Super
On executing the program, you will get the following
result −
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
THANK YOU

inheritance in Java with sample program.pptx

  • 1.
    INHERITANCE Mrs.G.Chandraprabha, Assistant Professor ofIT V.V.Vanniaperumal College for Women Virudhunagar
  • 2.
     Inheritance canbe defined as the process where one class acquires the properties (methods and fields) 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) and the class whose properties are inherited is known as superclass (base class, parent class).  Extends Keyword  extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.
  • 3.
    Syntax class Super { ..... ..... } classSub extends Super { ..... ..... }
  • 4.
    • Sample Code •Following is an example demonstrating Java inheritance. In this example, you can observe two classes namely Calculation and My_Calculation. • Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of Calculation class. • Copy and paste the following
  • 5.
    EXAMPLE :- 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); } } 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); } }
  • 6.
    Compile and executethe above code as shown below. • javac My_Calculation.java • java My_Calculation After executing the program, it will produce the following result − Output:- • The sum of the given numbers:30 • The difference between the given numbers:10 • The product of the given numbers:200
  • 7.
    • In thegiven program, when an object to My_Calculation class is created, a copy of the contents of the superclass is made within it. That is why, using the object of the subclass you can access the members of a superclass. Multiplication() Int z addition() Subtraction() Copy of calculation object Object of MY- CALCULATION CLASS
  • 8.
    • The Superclassreference variable can hold the subclass object, but using that variable you can access only the members of the superclass, so to access the members of both classes it is recommended to always create reference variable to the subclass. • If you consider the above program, you can instantiate the class as given below. But using the superclass reference variable ( cal in this case) you cannot call the method multiplication(), which belongs to the subclass My_Calculation. Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Subtraction(a, b);
  • 9.
    Note − Asubclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. The super keyword. The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used. ● 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. Differentiating the Members
  • 10.
    If a classis 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(); Sample Code This section provides you a program that demonstrates the usage of the super keyword.
  • 11.
    • In thegiven program, you have two classes namely Sub_class and Super_class, both have a method named display() with different implementations, and a variable named num with different values. We are invoking display() method of both classes and printing the value of the variable num of both classes. Here you can observe that we have used super keyword to differentiate the members of superclass from subclass. • Copy and paste the program in a file with name Sub_class.java.
  • 12.
    Example Live Demo 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; // display method of sub class public void display() { System.out.println("This is the display method of subclass"); } public void my_method() { // Instantiating subclass Sub_class sub = new Sub_class(); // Invoking the display() method of sub class sub.display(); // Invoking the display() method of superclass super.display(); // printing the value of variable num of subclass System.out.println("value of the variable named num in sub class:"+ sub.num); // printing the value of variable num of superclass System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]) { Sub_class obj = new Sub_class(); obj.my_method(); } }
  • 13.
    Compile and executethe above code using the following syntax. javac Super_Demo java Super On executing the program, you will get the following result − 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
  • 14.