You are on page 1of 10

INHERITANCE IN JAVA

IS-A RELATIONSHIP
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs.
When you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

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.
 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.

SALAR FADHIL 73
INHERITANCE IN JAVA
THE SYNTAX OF JAVA INHERITANCE
class Subclass-name extends Superclass-name{ class Employee{
//methods and fields float salary = 40000;
} }
class Programmer extends Employee{
int bonus = 10000;

In the example, Programmer object can public static void main(String args[]){
access the field of own class as well as of
Employee class i.e. code reusability. Programmer p = new Programmer();

System.out.println("Programmer salary is:"+ p.salary);


Why use inheritance in java System.out.println("Bonus of Programmer is:"+p.bonus);
 For Method Overriding (so runtime }
polymorphism can be achieved). }
 For Code Reusability. Output:
Programmer salary is: 40000.0
SALAR FADHIL
Bonus of programmer is: 10000 74
// Accessing superclass instance variable using super // Accessing superclass and subclass members using
keyword. subclass object reference variable.

public class Number { public class Parentclass {


int x = 20; void method1(){
void display() { System.out.println("Superclass method _1_");
System.out.println("X = " +x); }
} }
} public class Childclass extends Parentclass {
public class Number2 extends Number { void method2(){
int x = 50; System.out.println("Childclass method _2_");
void display() { }
System.out.println("X = " + super.x); }
System.out.println("X = " +x); public class InheritanceTest {
} public static void main(String[] args) {
} // Creating an object of superclass.
public class SuperTest { Childclass child = new Childclass();
public static void main(String[] args) { child. method1();
Number2 n = new Number2(); child. method2();
n.display(); }
} Output: } Output:
} X = 20 Superclass method_1_
SALAR FADHIL X = 50 Childclass method_2_ 75
INHERITANCE IN JAVA
TYPES OF INHERITANCE IN JAVA
On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.

SALAR FADHIL 76
INHERITANCE IN JAVA
TYPES OF INHERITANCE IN JAVA
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn
about interfaces later.
When one class inherits multiple classes, it is known as multiple inheritance.

Note: Multiple inheritance is not supported in Java through class.

SALAR FADHIL 77
INHERITANCE IN JAVA
SINGLE INHERITANCE EXAMPLE
creating a subclass from a single superclass is called class Animal{
single inheritance. In single-level inheritance, there is only void eat(){
one base class and can be one derived classes. System.out.println(“Animal eating...");
}
}
class Dog extends Animal{
void bark(){
System.out.println(“Dog barking...");
}
}
class SingleInheritance {
public static void main(String args[]){
Dog d = new Dog();
d.bark();
Output:
d.eat();
Dog barking...
} Animal eating...
}
SALAR FADHIL 78
class ChwarGosha{
int x;
void calcChwar(int la){
INHERITANCE IN JAVA x = la;
SINGLE INHERITANCE EXAMPLE }
System.out.println("robare chwar gosha : "+x * x);

class SingleInheritance{ }
static int num1 = 10; class Lakesha extends ChwarGosha {
static int num2 = 5; int y;
}
void calcLakesha(int la1, int la2){
class MainInheritance extends SingleInheritance{ x = la1;
y = la2;
public static void main(String[ ] args){ System.out.println("robare lagesha : "+ x * y);
}}
int num3 = 2; class SingleInheritance_3{
int result = num1 + num2 + num3; public static void main(String[ ] args) {

System.out.println("Result of child class is : "+result); Lakesha area2 = new Lakesha();


}
} area2.calcLakesha(20, 10);
area2.calcChwar(20); Output is:
}
Output is: robare lagesha : 200
}
Result of child class is 17 robare chwar gosha : 400
SALAR FADHIL 79
class Animal{
void eat(){
INHERITANCE IN JAVA System.out.println(“Animal eating...");
MULTILEVEL INHERITANCE EXAMPLE }
}
When there is a chain of inheritance, it is known as multilevel class Dog extends Animal{
inheritance. void bark(){
In multilevel inheritance, there is one base class and one derived System.out.println(“Dog barking...");
class at one level. At the next level, the derived class becomes
}
the base class for the next derived class and so on. This is as
shown below in the diagram. }
class BabyDog extends Dog{
void weep(){
System.out.println(“Baby Dog weeping...");
}
}
class MultilevelInheritance {
public static void main(String args[]){
BabyDog d = new BabyDog();
d.weep();
d.bark(); Output:
Baby Dog weeping...
d.eat();
Dog barking...
SALAR FADHIL
}} Animal eating... 80
class Animal{
void eat(){
INHERITANCE IN JAVA System.out.println("Animal eating...");
HIERARCHICAL INHERITANCE }
}
When two or more classes inherits a single class, it is class Dog extends Animal{
known as hierarchical inheritance. void bark(){
In this kind of inheritance, one class can be a parent of many System.out.println("Dog barking...");
other classes. }
}
class Cat extends Animal{
void meow(){
System.out.println("Cat meowing...");
}
}
class HierarchicalInheritance{
public static void main(String args[]){
Cat c = new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
SALAR FADHIL 81
INHERITANCE IN JAVA
Q) WHY MULTIPLE INHERITANCE IS NOT SUPPORTED IN JAVA?
To reduce the complexity and simplify the language, class A{
multiple inheritance is not supported in java. void msg(){
System.out.println("Hello A");
}
}
class B{
void msg(){
System.out.println("Welcome B");
}
}
class C extends A , B{
public static void main(String args[]){
C obj = new C();
obj.msg();
}
} Outpute:
SALAR FADHIL
Compile Time Error 82

You might also like