0% found this document useful (0 votes)
140 views

Inheritance in Java

Uploaded by

Rudranil Lahiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Inheritance in Java

Uploaded by

Rudranil Lahiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Computer Science: RNSMHS Class XII- Inheritance Notes

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.

Why use inheritance in java

 For Method Overriding (so runtime polymorphism can be achieved).


 For Code Reusability.

Syntax of Java Inheritance


1. class Subclass-name extends Superclass-name
2. {
3. //methods and fields
4.}
The extends keyword indicates that you are making a new class that derives from an existing class.
The meaning of "extends" is to increase the functionality.
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is: "+p.salary);
System.out.println("Bonus of Programmer is: "+p.bonus);
}
}

Output: Programmer salary is: 40000.0


Bonus of programmer is: 10000

Types of inheritance in Java:

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

1. Single Level Inheritance


When a class extends another one class only then we call it a single inheritance. The below
flow diagram shows that class B extends only one class which is A. Here A is a parent class of
B and B would be a child class of A.
Example:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output: barking...
eating...

2. Multilevel Inheritance Example


When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in
the example given below, BabyDog class inherits the Dog class which again inherits the Animal
class, so there is a multilevel inheritance.
Example:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}

2|Page
Department of Computer Science: RNSMHS Class XII- Inheritance Notes

class BabyDog extends Dog


{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output: weeping…
barking...
eating...

3. Hierarchical Inheritance Example


When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example
given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.
Example:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}

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

Why multiple inheritance is not supported in java?

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

Super Keyword in Java


The super keyword in java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which
is referred by super reference variable.
Usage of java super Keyword
 super can be used to refer immediate parent class instance variable.
 super can be used to invoke immediate parent class method.
 super() can be used to invoke immediate parent class constructor.
Super is used to refer immediate parent class instance variable.
Example:

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

Final Keyword in Java


The final keyword in java is used to restrict the user. The java final keyword can be used in many
context.
Final can be:
 Variable
 Method
 Class

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

Method Overriding in Java


If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in java.
Java Method Overriding
 Method overriding is used to provide specific implementation of a method that is already
provided by its super class.
 Method overriding is used for runtime polymorphism

Rules for Java Method Overriding


 method must have same name as in the parent class
 Method must have same parameter as in the parent class.
 Must be IS-A relationship (inheritance).

Example of method overriding


class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
}
}
Output: Bike is running safely

Getter & Setter Functions in Java:


Getter: It is a member method which is used to return the values of Private members of the
base class in derived class.
Setter: It is a member method which is used to feed the values of Private members of the
base class from the values passed from derived class.

********************************************************************
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

You might also like