Wednesday, February 1, 2012

Complex Program of Inheritence in Java

Q1)Output of the program.
class base
{
  public void hello()
  {
    System.out.println("I am base hello");
  }

};

class derive extends base
{
 public void hello()
 {
    System.out.println("I am derive hello");
    super.hello();
 };

};
class main
{
  public static void main(String args[])
  {
    derive d=new derive();
    d.hello();
  }
};
Output:-I am derive hello
        I am base hello
Discussion:-Here hello function is overridden.Derive hello function is calling to base hello function using super
            keyword.

Q2)output of the program.
class base
{
 private int a;
};

class derive extends base
{
 public void print()
 {
    a=100;
    System.out.println(a);
 };

};
class main
{
  public static void main(String args[])
  {
    derive d=new derive();
    d.print();
  }
};
Output:-Run time error.
Discussion:-Here a data member is a property of base class and declared as private data member.
            So from derive class it is not possible to access a member.As we know private member is
            not inheritable.


           
Q3)Output of the program
class A
{
 public int a;
};

class B extends A
{
 public int b;
};
class C extends B
{
 void getdata()
 {
  a=100;
  b=200;
  System.out.println(a);
  System.out.println(b);
 }
};

class main
{
  public static void main(String args[])
  {
    C c=new C();
    c.getdata();
  }
};
Output:-100 200
Discussion:-It is a example of multilevel inheritence.B class is derive from A and C is derive from B.So getdata() function which is
             a public methode of C class can able to access the public property of A and B class.
           
Q4)Output of the program
class A
{
  protected static void hello()
   {
    System.out.println("I am A hello");
   };
};

class B extends A
{
 protected static void callhello()
 {
   hello(); 
 };

};
class main
{
  public static void main(String args[])
  {
    B b=new B();
    b.callhello();
  }
};
Output:-I am A hello.
Discussion:-Protected callhello() methode is calling another protected methode hello() in base class.

Q5)Output of the program.
class A
{
};

class B
{
};

class C extends A, extends B
{
};

class main
{
  public static void main(String args[])
  {
    C c=new C();
  }
};
Output:-Compile error.
Discussion:-Here C class is trying to derive from both A and B.But Java do not support multiple inheritence.
       
       
Q6)Output of the program
interface abc
{
  final int a=100;
};

class xyz implements abc
{
  void print()
  {
    System.out.println(a);
  };
};

class main
{
  public static void main(String args[])
   {
     xyz a=new xyz();
     a.print();
   };
};
Output:-100
Discussion:-Here abc interface perform like base class.And xyz class is implemented from abc interface.


Q7)Output of the program
interface abc
{
 void hello()
  {
    System.out.println("Hello world");
  };
};

class xyz implements abc
{
 
};
class main
{
  public static void main(String args[])
   {
     xyz a=new xyz();
     a.print();
   };
 
};
Output:-Compile time error.
Discussion:-Interface do not contain function body.Only prototype declaration is allowed here.

Q8)Output of the program
interface abc
{
 final int a=100;
};

interface pqr
{
 final int b=200;
};

class xyz implements abc,pqr
{
  void print()
   {
    System.out.println(a+b);
   };
 
};
class main
{
  public static void main(String args[])
   {
     xyz a=new xyz();
     a.print();
   };
 
};
Output:-300
Discussion:-xyz class is implemented from the interface abc and pqr. It is like multiple inheritence of C++.


Q9)Output of the program
interface abc
{
 void test1();
};
interface pqr extends abc
{
 void test2();
};

class xyz implements pqr
{
  public void test1()
  {
    System.out.println("I am test1");
  };
  public void test2()
  {
    System.out.println("I am test2");
  };
};

class main
{
  public static void main(String args[])
   {
     xyz a=new xyz();
     a.test1();
     a.test2();
   };
 
}
Output:-I am test1
        I am test2
Discussion:-Here pqr interface is extended from abc interface and xyz class is extended from pqr interface.
            so from xyz class it is possible to access all the property of abc and pqr interface.
           
Q10)Output of the program
interface fline
{
 void showline();
};
class nov1 implements fline
{
  public void showline()
  {
    System.out.println("To be or not to be");
  };
};
class nov2 implements fline
{
  public void author()
  {
    System.out.println("Shskespeare");
  };
};
public class main
{
  public static void main(String args[])
   {
     nov1 hamlet=new nov1();
     nov2 juliet=new nov2();
   
     hamlet.showline();
     juliet.author();
   };
 
};
Output:-Compile error
Discussion:-Here showline() methode need to define within nov2 class.Otherwise nov2 class must have to declare finally.


Q11)Output of the program
interface shape
{
 void draw();
};

interface circle extends shape
{
 void getredious();
 int redious=10;
};

class newcircle implements circle
{
  public void draw()
  {
 
  };
  public void getredious()
  {
    System.out.println(redious);
  };
};

public class main
{
  public static void main(String args[])
   {
    newcircle cr=new newcircle();
    cr.getredious();
   };
 
};
Output:-10
Discussion:-newcircle can access both the property of shape and circle interface,As it is derived from circle.

Q12)Output of the program
interface shape
{
 void draw();
};

class circle1 implements shape
{
  public void draw()
  {
    System.out.println("I am circle1 function");
  };
};

class circle2 extends circle1
{
  public void draw()
  {
    System.out.println("I am circle2 function");
  };
};
public class main
{
  public static void main(String args[])
   {
    circle1 c1=new circle1();
    circle2 c2=new circle2();
    c1.draw();
    c2.draw();
   
   };
};
Output:-I am circle1 function
        I am circle2 function
Discussion:-circle1 class is implemented from shape interface.Ans circle2 class is ectendex from circle1 class.So draw() methode is overridden
            .It id declared both in circle1 and circle2 class.
     



               
           


No comments:

Post a Comment