Java Constructor and Inheritance

 

JAVA

Constructor and Inheritance

 


Fill in the blanks

 

1. Constructor means Creating an object

2. Destructor means Deleting an object

3.Class name should be same as method name is called Constructor

4.Constructor should not have any return value/type.[T/F]. True

5.Constructor can’t be Overloaded.[T/F]. False

6.Constructor without argument is called Default Constructor

7.Constructor with argument is called Parameterized Constructor

8.Constructor with the argument will be an object is called Copy Constructor

9.This keyword used to refer the current object.

10.Write access specifiers public,  private , protected and  default

11.Write a keyword for inheritance extends

 

Answer the Following Questions

                                                                (4 marks)

                1.Write a program for default and parameterized Constructor

class Person{

    String name;

   int age;

   Person()

   {

     name="Unknown";

     age=0;

}

  Person(String n, int a){

    name=n;

    age=a;

}

 void show(){

       System.out.println("Name: "+name+" , Age: "+age);

}

public static void main(String args[]){

  Person p;

  p=new Person();  

  p.show();

  p=new Person("Babu",35);

  p.show();

   }

}

        2.Write a program for Static Method

class Recta{

   int length;

   int breadth;

   static int count;

   Recta() {

        count++;

    }

    static {

       count=0;

            System.out.println("Inside static block");

    }

    static void displayCount()

    {

                 System.out.println("No of Object"+count);

   }

   public static void main(String args[])

   {

       Recta r1, r2;

       Recta.displayCount();

       r1=new Recta();

       r1.displayCount();

       r2=new Recta();

      Recta.displayCount();

      }

}

        3.Write a program for Single Inheritance

File name: A.java

class A{

              int i, j;

              void showij(){

         System.out.println("i and j:"+i+"  "+j);

             }

}

// Create a subclass by extending class A.

File name: B.java

class B extends A{

                 int k;

                 void showk() {

                     System.out.println("k : "+k);

                }

                void sum() {

      System.out.println("i+j+k:  "+(i+j+k));

               }

}

 

File name: SimpleInheritance.java

class SimpleInheritance{

public static void main(String args[]){

       A superOb=new A();

       B subOb=new B();

       //The superclass may be used by itself.

      superOb.i=10;

      superOb.j=20;

     System.out.println("Contents of superOb:");

     superOb.showij();

     System.out.println();

    /*The subclass has access to all public members fits superclass.*/

    subOb.i=7;

    subOb.j=8;

    subOb.k=9;

    System.out.println("Contents of subOb:");

    subOb.showij();

    subOb.showk();

    System.out.println();

    System.out.println("Sum of i , j and k in subOb:");

    subOb.sum();

    }

}

 

       

Answer the Following Questions

                                                                (10 marks)

        1.Write a program for Constructor Overloading

File name: Cons.java

class Cons{

       double width,height,depth;

       Cons(double w,double h,double d){

              width=w;

              height=h;

              depth=d;

      }

     //constructor used when no when no dimensions specified

    Cons() {

    width=-1;          //use-1 to indicate

    height=-1;       //an uninitialized

    depth=-1;       //box

    }

    //contructor used when cube is created

   Cons(double len) {

    //this can be used to call different constructor from same class.

   this(len,len,len);

   }

   void volume()

  {

      System.out.println("volume:"+width*height*depth);

  }

}

File name: ConsOverload.java

class ConsOverload {

   public static void main(String args[]) {

      Cons mybox1=new Cons(10,20,15);

      Cons mybox2=new Cons();

      Cons mycube=new Cons(7);

     mybox1.volume();

     mybox2.volume();

     mycube.volume();

     }

}

 

        2.Write a program for Method Overriding

File name: Figure.java

class Figure{

         double dim1;

         double dim2;

         Figure(double a, double b) {

                    dim1 =a;

                    dim2 =b;

        }

        double area() {

            System.out.println("Area for Figure is undefined.");

            return 0;

        }

}

File name: Rectangle.java

class Rectangle extends Figure {

         Rectangle(double a,double b) {

         super(a,b);

         }

         // override area for rectangle

         double area() {

         System.out.println("Inside Area for Rectangle.");

         return dim1* dim2;

         }

}

File name: Triangle.java

class Triangle extends Figure {

          Triangle(double a, double b) {

                  super(a, b);

           }

         //override area for right triangle

         double area() {

         System.out.println("Inside Area for Triangle.");

         return dim1 * dim2/ 2;

         }

}

File name: FindAreas.java

class FindAreas{

             public static void main(String args[]) {

            Figure f =new Figure(10, 10);

            Rectangle r =new Rectangle(9,5);

           Triangle t = new Triangle(10, 8);

           Figure figref;

           figref = r;

           System.out.println("Area is" + figref.area());

           figref = t;

           System.out.println("Area is" + figref.area());

           figref = f;

            System.out.println("Area is" + figref.area());

          }

}

        3.Write a program for Single Inheritance using Super keyword.

File name: Book.java

import java.lang.*;

import java.io.*;

class Book

{

      int bno;

      String bname;

      double price;

      public Book(int n,String bn,double p)

     {

                 bno=n;

                 bname=bn;

                 price=p;

    }

    void display()

    {

    System.out.println("\nBook Number = " + bno);

       System.out.println("\nBook Number = " + bname);

          System.out.println("\nBook Number = " + price);

         }

}

File name: Purchase.java

class Purchase extends Book

{

      int qord;

      double netcost;

      public Purchase(int n1,String bn1, double p1,int q)

      {

      super(n1,bn1,p1); //Base Class Constructor

     qord=q;

     }

     void display()  //Overriding of methods

     {

     netcost=qord*super.price; //Baseclass Variable

     super.display();     //Base class Function

     System.out.println("\nQuantity ordered = "+qord);

     System.out.println("\nNet cost="+netcost);

      }

}

File name: Inherit1.java

class Inherit1

{

     public static void main(String args[])

     {

     Purchase pur =new Purchase(10,"Black book of java",500.00,34);

     pur.display();

     }

}

 

No comments:

Post a Comment