/* Write a JAVA Program to demonstrate Inheritance.*/
// InheritMain.java
class SuperClass
{
int a,b;
SuperClass(int x,int y)
{
a=x;
b=y;
}
void show()
{
System.out.println("In Super Class");
System.out.println(" A and B are " + a + " " + b);
}
}
class SubClass extends SuperClass
{
int ans;
int add;
SubClass(int a,int b,int c)
{
super(a,b);
ans=c;
}
void show()
{
System.out.println("In Sub Class");
System.out.println("C value is: " + ans);
super.show();
add=a+b+ans;
System.out.println("Addition of A B and C :" + add);
}
}
class InheritMain
{
public static void main(String args[])
{
SubClass ob=new SubClass(10,20,30);
ob.show();
}
}
// InheritMain.java
class SuperClass
{
int a,b;
SuperClass(int x,int y)
{
a=x;
b=y;
}
void show()
{
System.out.println("In Super Class");
System.out.println(" A and B are " + a + " " + b);
}
}
class SubClass extends SuperClass
{
int ans;
int add;
SubClass(int a,int b,int c)
{
super(a,b);
ans=c;
}
void show()
{
System.out.println("In Sub Class");
System.out.println("C value is: " + ans);
super.show();
add=a+b+ans;
System.out.println("Addition of A B and C :" + add);
}
}
class InheritMain
{
public static void main(String args[])
{
SubClass ob=new SubClass(10,20,30);
ob.show();
}
}
No comments:
Post a Comment