3 types inheritance: Simple Inheritance

3 types inheritance available in java programs. In c++ we can manage 5 types of inheritance.

3 Types Inheritance

  1. Simple Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

 

In this blog, we will work on Simple Inheritance. This is simple structure of Simple Inheritance. A is Super class and B is sub class. Sub class B can access all non-private data member (variables and methods).

A             Super class

|

B             Sub class

 

class A   // Super class

{

int x,y;  // Super class variable

void setA(int a,int b)

{

x = a;

y = b;

}

void displayA()

{

System.out.println(“Sum = ” + (x+y));

}

}

class B   extends A           // Join this class with A | extends – keyword

{

// Want to use super class variable in sub class

void setB(int a,int b)

{

x = a;     // because of inheritance, we can use super class variable

y = b;

}

void displayB()

{

System.out.println(“Sub = ” + (x-y));

}

}

class inh1

{

public static void main(String args[])

{

// We can call super class method using sub class object

B obj = new B();                // object of sub class

obj.setA(20,10);               // call to super class method

obj.displayA();                  // call to super class method

obj.setB(50,10);                // call to own | sub class method

obj.displayB();                   // call to own | sub class method

}

}

OutPut

Sum = 30

Sub = 40

Use of inheritance

We can use super class properties [variables]

We can call and access super class method and variable using sub class object

 

Video available on YouTube.


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

776total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

1 Comment

  1. October 14, 2016 - 7:53 pm

    Thanks for one’s marvelous posting! I definitely enjoyed reading it,
    you are a great author.I will make certain to bookmark your blog
    and will often come back at some point. I want to encourage that you continue your great job, have a nice day!

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint