How to Inherit The Properties Of One Class To Another Class In Java?

We will learn How to Inherit The Properties Of One Class To Another Class In Java? where one class inherited the property of another using the keyword Extends which inherited all the properties of the parent class to the child class.

 Inherit The Properties Of One Class

Inheritance

Just like its literal meaning inheritance means getting the properties of one(Parents) to another(Child) a similar concept is followed here. As if we want to use the functions and variables of one class to another then we can do so by simply inheriting the parent class to the child class.

Let’s take an example and try to understand it.

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);  
}  
}

Here we can see that the class Employee has one variable which is salary and there is another class called programmer which has its own variable bonus. Now we can easily see that Programmer extends the class Employee.

Here Employee is a Parent class and Programmer is a child class, Let’s understand the terms Parent class and Child Class.

Parent Class

The class is being extended by some other class in the above example employee is parent class as it is being extended by another class that we call Programmer and as Child Class.

Child Class

They are the classes that inherited the properties of the parent class which means the child class has access to the functions and variables of both classes. Moreover child class can access all the resources so the object of a child class can be used for calling the functions of the parent class also which we call upcasting.

It is an advantage of creating an object of the child class that we can call the function of the child as well as parent class functions. But visa-versa is not possible as an object of the parent class can not call the functions of the child class as it does not have any right to do so

 

Learn More about How to Inherit The Properties Of One Class To Another Class In Java?: Inheritance in OPPs

And also visit JAVA TUTORIAL for learning more about java and solving all your problems related to java.

 

Leave a Comment

%d bloggers like this: