Recent

9.Inheritance in Java

Inheritance In Java

To acquire properties and behaviour of objects of one class by another class is known as inheritance.

The class who inherits a particular class is called child class and the class to whom it inherit is known as parent class. A child class can reuse methods and fields of parent class, and we can also add new methods and fields in child class.

Example: Car engine- It is made once and used again and again.

Two major advantages of inheritance

1.    Code re usability: we can reuse the code from parent class.

2.    To achieve dynamic binding(Runtime polymorphism): By method overriding.

Types of Inheritance

1.    Single Inheritance: It is simple inheritance in which a class inherits properties and behaviour from a single class.

2.    Multilevel Inheritance: As the name suggest, One class(child class) is inherits other class(parent class) and the child class is inherited by some other class and so on.

3.    Hierarchical inheritance: In this type of inheritance, a single class(parent class) is inherited by more than two classes.

4.    Hybrid Inheritance:

5.    Multiple Inheritance: In this type of inheritance, a single class inherits more than one class. It means there are more than one parent class for a single child class.

Multiple inheritance is not supported in Java.

 

>>Now the question arises why it is not supported in Java?

Answer: To reduce the complexity and to make the Java language more secure, multiple inheritance is not supported in Java.

POINTS TO REMEMBER

1.    By default all the data members and member functions of a parent class are available to child class except private data member/member function.

2.    Inheritance always occurs at runtime.

Question: Inheritance occurs at compile time or runtime?

Answer: Inheritance always occurs at runtime.

Question: Whenever we create the object of a child class then the object of parent class will be created automatically or not?

Answer: No, everything is acquired by the child class at runtime and they will also get the memory in child class area.

How can we use Inheritance in Java (Syntax):

Java technology provides a keyword “extends” to achieve inheritance in Java. Here is the syntax to use “extends” keyword.

———–> class Child extends Parent

A basic example of inheritance:

 

 

class Base

{

int x = 10;

 

void show()

{

System.out.println("Show function from class Base");

}

}

 

class Child extends Base   //child class inherits class Base

{

 

public static void main(String[] args)

{

Child c = new Child();

System.out.println(c.x); //value of x from class Base will be printed

c.show();  //show function from Base class will be executed

}

}

Output:

What is Data Hiding

Whenever a parent class and a child class both are having a same data member, then this concept is known as data hiding. The parent class variable hides behind child class variable and if we print the value of the variable through child class then it always prints the child class variables.

Consider this program to understand the concept of data hiding:

 

 

class Base

{

int x = 10;

}

 

class Child extends Base

{

int x = 120;

 

void show()

{

System.out.println("variable x value:" +x);

}

 

public static void main(String[] args)

{

Child c = new Child();

c.show();

}

}

 

/* Output

variable x value: 120

*/

In this program if we will print the value of “x” like this then it will print the Child class variable always but if we want to use the variable of parent class then we will have to use a keyword “super”. See the example again with a small change.

 

 

class Base

{

int x = 10;

}

 

class Child extends Base

{

int x = 120;

 

void show()

{

System.out.println("variable x value:" +x);

System.out.println("variable x value via super:" +super.x);

}

 

public static void main(String[] args)

{

Child c = new Child();

c.show();

}

}

Output:

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India