This article may useful to you if you’re not aware of the term inheritance!Inheritance the word defines itself,inherit means get from other.In programming language we are able to say inheritance means that child class get each accessible property of parent class!Now the question is there what’s parent class and child class.

Parent class is the base class that have its own property means it have some variables,methods,constructors etc.Child class may be defined as the can be generated from base class or we are able to say the class which uses the property of base class,child class even have its own variables,methods,constructors…

When child class inherit parent class that means it have accessible permission to use parent class property.The keyword extends is used between child class and parent class.

Also read:-  Switch statement in java
class child_class extends parent_class;

Suppose we have parent class and child class like this

class a{
variables;
methods;
}
class b extends class a
{
variable2;
methods2;
}

Here we can say that class b can use variables and methods of class a easily if it have access permission.Though child class also have its own set of variables and methods that can not be used by parent
class.Here is simple inheritance program in java that you can easily access.

Code

class temp1{
int a,b;
void showtemp1()
{
System.out.println(“a: “+a+” b: “+b);
}
}
class temp2 extends temp1{
int c,d;
void showtemp2()
{
showtemp1();
System.out.println(“c: “+c+” d: “+d);
}
}
class inherit1{
public static void main(String args[])
{
temp2 t=new temp2();
t.a=1;
t.b=2;
t.c=3;
t.d=4;
t.showtemp2();
}
}

Output

a: 1 b: 2
c: 3 d:4

Logic part

Parent class have two publicly defined variables and that can be printed using its own function/method.Here another class is temp2 which inherits all the property of class temp1.So if we create object of child class and try to use property of parent class then it will be executed easily.In child class method we have called base class’s method to print three value.Now in the main class we simply created object of child class and tried to access all property within base and derived class.

Member access in inheritance

In upper example we saw that derived class can access all the property of parent class, but it was for publicly derived methods and variable.In case of private methods and variable child class can never execute of its private methods or variables.So if we want to use private variable then we have to use some publicly defined methods of that class.In java when you does not specify access property then it is considered as public.

Also read:- Ternary operator in java
You may get better idea about access property and how to use it in inheritance in below program

Code

class temp1{


int a;
private int b;


void set(int x,int y)
{
a=x;
b=y;
}
int getb()
{
return b;
}
}
class temp2 extends temp1{
void mul()
{
System.out.println(“Multiplication : “+(a*getb()));
}
}
class inherit2{
public static void main(String args[])
{
temp2 t=new temp2();
t.set(10,20);
t.mul();
}
}

Output

Multiplication : 200

Logic part

It’s going same as above but the different is that one variable is private in base class.So we have declared one public method which simply returns it’s private variable and used that method in child class .Finally we created object of derived class and printed the required result.

Yet not got the exact idea about inheritance then you should try the below program.It is complicated example but it will help you to clear your concept…

Code

//Parent class or base class
class box1{
int w;
int h;
int d;


box1(box1 b) //Constructor with object
{
w=b.w;
h=b.h;
}
box1(int w1,int h1,int d1)  //parameterized constructor
{
w=w1;
h=h1;
d=d1;
}
box1() //Default constructor
{
w=1;
h=1;
d=1;
}
void volume()
{
System.out.println(“Volume of box : “+w*h*d);
}
}
//child class or derived class
class box2 extends box1    //Inherits class box1
{
int e;
box2(int w1,int h1,int d1,int e1) //Constructor of child class
{
w=w1;
h=h1;
d=d1;
e=e1;
}
}
//Main class starts now//
class inherit4{         
public static void main(String args[])
{
box2 obj1=new box2(10,20,30,40);
box2 obj2=new box2(1,2,3,4);
box1 obj3=new box1(obj2);  //Use object as parameter in constructor


obj1.volume();
obj3.volume();
}
}

Output

Volume of box : 300
Volume of box : 6

Logic part

In child class there are three type of constructor we have defined .First constructor will take object as parameter and with help of it you can initialise the variables.In second constructor we used variables to initialise member variables, that constructor is called parameterized constructor and the third one is default constructor which uniquely initialise variables value.Now in child class we have defined another parameterized constructor to initialise both class variable.Now in main class  we have created two object.First object will simply prints the result.But for second object,it will used as parameter in firstly defined constructor and then prints the final result…

So this it the tutorial about inheritance in which we have discussed about how to use inheritance feature and also learnt about member access in inheritance.Don’t forget  to like our fbpage to get more daily tutorials about java,Also share it with your friends and Follow us on google.

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *