Thursday, December 19, 2013

Constructor with Overloading in JAVA : Day 8

          a) How to Overload a Constructor? Demonstrate with example.
    
      b) How to use Object as Parameter? Give example.

5 comments:

  1. a))) *** How to Overload a Constructor ? Demonstration with example.

    =>

    Constructor overloading is not complex, just need to create another constructor, obviously same name as of class but

    different signature but there are certain rules related to Constructor overloading which needs to be remembered while

    overloading constructor in Java. One Constructor can only be called from inside of another Constructor and if called it

    must be first statement of that Constructor.

    example:

    public class Box
    {
    private int height;
    private int width;
    private int depth;

    // first constructor with no parameter //

    Box()
    {
    height=0;
    width=0;
    depth=0;
    }

    // second overloaded constructor //

    Box(int h,int w,int d)
    {
    height = h;
    width = w;
    depth = d;
    }
    public void display()
    {
    System.out.println(height+widht+depth);
    }
    public static void main(String args[])
    {
    Box object = new Box(1,2,3);
    object.display();
    }
    }

    ReplyDelete
  2. *******How to use Object as Parameter? Give example.*******
    Solution:

    Source code:

    public class Rectangle
    {
    int length;
    int width;

    Rectangle(int l, int w)
    {
    length = l;
    width = w;
    }

    void area(Rectangle r1)
    {
    int areaofRectangle = r1.length*r1.width;
    System.out,println("Area of Rectangle : " + areaofRectangle);
    }
    }
    class RectangleDemo
    {
    public static void main(String args[])
    {
    Rectangle r1 = new Rectangle (20,10);
    r1.area(r1);
    }
    }

    ReplyDelete
  3. ********How to Overload a Constructor? Demonstrate with example.**********
    Ans: When more than a single constructor is defined in a class, it is known as constructor overloading. Similarly, when more than one method with the same name is defined in the same class, it is known as method overloading. However, there is a restriction on such overloading. Constructors or methods can be overloaded only in a way that would allow calls to these constructors or methods to be resolved i.e. when an overloaded method is invoked, the compiler should be able to decide which of the overloaded methods is to be called by checking the arguments. For example consider an overloaded method print () having two versions. The first of these does not take any arguments while the second one takes a single argument. A statement like the following is a call to definitely the second version of the overloaded method which requires a single String argument.


    Example:

    public class perimeter
    {
    public perimeter()
    {
    System.out.println(“from default”);
    }
    public perimeter(int x)
    {
    this();
    System.out.println(“Circle Perimeter: ” + 2*Math.PI*x);
    }
    public perimeter(int x, int y)
    {
    this(100);
    System.out.println(“Rectangle Perimeter: ” + 2*(x+y));
    }
    public static void main(String args[])
    {
    perimeter p3 = new perimeter(15,25);
    }
    }

    ReplyDelete
  4. Thanks for Information Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications or applets. There are many websites and lots of applications that will not work unless you have Java installed, and more are created every day. Java is the fast, secure and more reliable. Java applications are used everywhere from laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet etc. JAVA Online Training

    ReplyDelete
  5. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training in India. or learn thru Java Online Training in India Students. or learn thru JavaScript Online Training in India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

    ReplyDelete