Java
Java
INHERITANCE
• Inheritance can be defined as the process where one
class acquires the properties 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 and the class whose properties are
inherited is known as superclass .
extends Keyword
• extends is the keyword used class Super{
to inherit the properties of a .....
class. Below given is the .....
syntax ofextends keyword. }
class Sub extends
• Syntax:- Super{
.....
.....
}
Sample Code
• Below given is an example demonstrating Java inheritance. In this example you can observe twoclasses
namely Calculation and My_Calculation.
• Using extends keyword the My_Calculation inherits the methods addition and Subtraction of
Calculation class.
• Copy and paste the program given below in a file with name My_Calculation.java
class Calculation{
int z; public class My_Calculation extends
public void addition(int x, int y){ Calculation{
z=x+y; public void m ultiplication(int x,
System .out.println("The sum of the int y){
given num bers:"+z); z=x* y;
} System .out.println("The product of
public void Substraction(int x,int the given num bers:"+z);
y){ }
z=x-y; public static void m ain(String
System .out.println("The difference args[]){
between the given num bers:"+z); Int a=20, b=10;
} My_Calculation obj= new
} My_Calculation();
obj.addition(a, b);
obj.Substraction(a, b);
TYPES OF INHERITANCE