You are here: Home / Topics / Simple Inheritance Example 2 in Java

Simple Inheritance Example 2 in Java

Filed under: Java on 2023-09-17 06:56:01

// Simple Inheritance example 2

class  A 
{
int i, j;

void showij() 
{
 System.out.println("i and j: " + i + " " + j);
}
}

class B extends A 
{
int k;

void showk() 
{
 System.out.println("k: " + k);
}

void sum() 
{
 System.out.println("i+j+k: " + (i+j+k));
}
}

class  InheritanceSimple 
{
public static void main(String args[ ]) 
{
 A sup = new A();
 B sub = new B();

 sup.i = 10;
 sup.j = 20;
 sup.showij();

 sub.i = 7;
 sub.j = 8;
 sub.k = 9;
 sub.showij();
 sub.showk();
 sub.sum();
}
}


Output:

i and j: 10 20
i and j: 7 8
k:9
i+j+k: 24

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.