Constructors in Java


This is the 17th post on my blog. This post had to be posted before. But I tried much to finish it quickly as I can. Enjoy with constructors. 


What is the constructor?

This is another valuable thing in programming. You may hear about constructor companies. What they are doing is constructing some things means building, roads or whatever. Likewise, in programming, there are also constructors. But they are not creating that buildings or roads. When you create objects, constructors are always invoked. If you use the 'new' keyword, constructors are running. 

Rules of constructor

  • Every class, even abstract classes must have a constructor. There can be one or more.
  • Interfaces don't have constructors.
  • The constructor name should be exactly the same as the class name.
  • The constructor does not have a return type.
  • Constructors can use any access modifiers (private, protected, default or public).

Default constructor

  • 'The default constructor' is created by default by the compiler if you don't create any constructors in your class, but if the programmer wants to create a constructor, he can type it as,  
                   Class ConstructorTest{
                           ConstructorTest() {  }
                   }

  • The default constructor is always a no-args constructor. But no-arg constructor is not necessarily the default.
  • Every constructor has, as its first statement either this() or super()
                   this()   -   overloaded constructor
                   super()   -   superclass constructor  

  • Default constructor provides a no-arg call to super(). It means if you don't type any constructor, it will be generated a default constructor and it create a no-arg call to super().
  • The default constructor provides the default values for objects. They may be 0 or null or etc.


public class DefaultCon {
     int id;
     float avg;
     double d;
     String name;
           
     void test(){
          System.out.println(id + " | " + name + " | " + avg + " | " + d);
     }
             
     public static void main(String args[]){
          DefaultCon obj1 = new DefaultCon();
          obj1.test();
     }
} 

In the above section we have talked about default constructor, super...etc. The following shows the auto-generated code for what you typed code.




Other constructors with parameters


public class OtherCon {
     int id;
     String name;
  
     OtherCon(int id, String name){
          this.id=id;

          this.name=name;
     }
 
     void display(){
          System.out.println(id + "   " + name);
     }
 
     public static void main(String args[]){
          OtherCon obj1 = new OtherCon(1,"John");
          OtherCon obj2 = new OtherCon(2,"Adam");
  
          System.out.println("ID  Name");
          obj1.display();
          obj2.display();
     }
}


Constructor Overloading


public class OverloadingCon {
     String name;
     int marks;
 
     OverloadingCon(String name, int marks){
          this.marks=marks;
          this.name=name;
     }
 
     OverloadingCon(String name){
          this.name=name;
     }
 
     void display(){
          System.out.println(name+ "  " + marks);
     }
 
     public static void main(String args[]){
          OverloadingCon obj1 = new OverloadingCon("Adam",80);
          OverloadingCon obj2 = new OverloadingCon("John");
  
          obj1.display();
          obj2.display();
     }
 }



Constructor chaining

  • This occurred when a class inherits another class.
  • You know that a subclass inherits the things in the superclass. 
  • When you create an object from a subclass, its constructor is invoked.
  • But the thing is, the superclass constructor needs to be invoked because of inheritance.
  • It is done by super() keyword and it can be done as follows.


class Fruit{
     String type; 
           
     Fruit(String type){
         this.type=type;
     }
}    


class Apple extends Fruit{

     String color;
     Apple(String type, String color){
          super(type); 
          this.color=color;
     }
 
     void display(){
          System.out.println("Fruit type is: " + type);
          System.out.println("Fruit color is: " + color);
     }
}   
public class ConstructorChain {

     public static void main(String args[]){
           Apple obj = new Apple("Sweet", "Red");
           obj.display();
     }
}



Click here to download above codes




Constructors in Java Constructors in Java Reviewed by Ravi Yasas on 11:09 PM Rating: 5

1 comment:

  1. Hey there, You have done an incredible job. I wil definitely digg it and personally suggest to my friends.
    I'm confident they'll be benefiited from this site.

    ReplyDelete

Powered by Blogger.