//Note: Analysis the programs errors,outputs and Understand Inheritance in Java
//[1]
class A{
public int i;
public void seti(int ii){
i=ii;
}
public void printi(){
System.out.println(i);
}
}
class B extends A{
public int j;
public void setj(int jj){
j=jj;
}
public void printj(){
System.out.println(j);
}
}
class Demo1{
public static void main(String arg[]){
B obj=new B();
obj.seti(5);
obj.setj(55);
obj.printi();
obj.printj();
}
}
//[2]
class A{
public int i;
public void seti(int ii){
i=ii;
}
public void printi(){
System.out.println("superclass i="+i);
}
}
class B extends A{
public int i;
public void seti(int ii){
i=ii;
super.seti(75);
}
public void printi(){
System.out.println("subclass i="+i);
super.printi();
}
}
class Demo2{
public static void main(String arg[]){
B obj=new B();
obj.seti(5);
obj.printi();
}
}
//[3]
class A{
public void print(){
System.out.println("i am from class A");
}
}
class B extends A{
public void print(){
System.out.println("i am from class B");
}
}
class Demo3{
public static void main(String arg[]){
A ob;
B ob1=new B();
ob=ob1;
ob.print();//B class print method
A ob2=new A();
ob=ob2;
ob2.print();//A class print method
}
}
//[4]
class Shape{
public void print(){
System.out.println("General class");
}
}
class Tri extends Shape{
public void print(){
System.out.println("Area of Traingle");
}
}
class Square extends Shape{
public void print(){
System.out.println("Area of the Square");
}
}
class Demo59{
public static void main(String arg[]){
Shape s;
Shape s1=new Shape();
Tri t1=new Tri();
Square sq=new Square();
s=s1;
s.print();//class shape print
s=t1;
s.print();//class Tri print
s=sq;
s.print();//class square print
}
}
No comments:
Post a Comment