Inheritance in Java
Inheritance in Java
INHERITANCE IN JAVA
As the name suggests, inheritance means inheriting properties. Inheritance in java is a mechanism in
which one class acquires all the properties and behaviours of parent class. Inheritance represents the
IS-A relationship, also known as parent child relationship.
In java, Multiple and Hybrid inheritance is not possible using Class. To achieve them we have
to use interface.
1|Page
Department of Computer Science: RNSMHS Class XII- Inheritance Notes
2|Page
Department of Computer Science: RNSMHS Class XII- Inheritance Notes
3|Page
Department of Computer Science: RNSMHS Class XII- Inheritance Notes
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//will give us Compile Time Error
}
}
Output: meowing...
eating...
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If, A and
B classes have the same method and you call it from child class object, there will be ambiguity to call
the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you
inherit 2 classes. So whether you have same method or different, there will be compile time error.
Example:
class A
{
void msg(){System.out.println("Hello");}
}
class B
{
void msg()
{
System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
public static void main(String args[])
{
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Output: Compile Time Error
Note: A subclass includes all of the members of its super class but it cannot access those members of
the super class that have been declared as private. Attempt to access a private variable would cause
compilation error as it causes access violation. The variables declared as private, is only accessible by
other members of its own class. Subclass have no access to it.
4|Page
Department of Computer Science: RNSMHS Class XII- Inheritance Notes
class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="black";
void printColor()
{
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal
class
}
}
class TestSuper1
{
public static void main(String args[])
{
Dog d=new Dog();
d.printColor();
}
}
Output: black
White
The final keyword can be applied with the variables, a final variable that have no value it is called
blank final variable or uninitialized final variable. It can be initialized in the constructor only. The
blank final variable can be static also which will be initialized in the static block only.
5|Page
Department of Computer Science: RNSMHS Class XII- Inheritance Notes
********************************************************************
Note: Programs discussed in Class are uploaded in the Classroom. Do
go through the same and similar questions will be discussed during
the revision classes.
6|Page