Hi Readers,
If you are beginner with Java then let me introduce your to constructor.
class Animal()
Having private constructor will not allow anyone outside of your class to create new object. This concept is used in Singleton pattern or class.
Argument-ed constructor are used to initialize instance variables in any class. Lets take an example here
If you are beginner with Java then let me introduce your to constructor.
Constructor
Constructor instantiate ( allocate memory and default values ) the defined instance variables while creating new object. There are certain rules comes with constructor.{ public Animal(){ System.out.println("This is coming from Animal Class."); } }
- constructor must have same name as class name.
- constructor will run every time when we make new object. Like new Animal(); In Animal class example you will see result statement "This is coming from Animal Class." will be printed.
- constructor can be default, public, private and protected also. Many a times it is public so that all of us can create object.
- constructor never returns anything. This is key difference between constructor and method.
class Animal() { // Constructor public Animal(){ System.out.println(" This is coming from Animal Class."); } // Method public void animal(){ System.out.println(" This is coming from Animal Class. But i am a method"); } }
- If you will not define any constructor in your class then JVM will create one for you when someone calls new object on your class.
- If you have created constructor in your class then JVM will not create any default constructor for you.
- Interface in java can not have constructor.
- Abstract class can have constructor because we can have implementations (Non-Abstract) things in our abstract class.
Example
class Animal() { // Constructor public Animal(){ System.out.println(" This is coming from Animal Class."); } public static void main(String args[]){ System.out.println(" This is coming from main method"); } }
Private Constructor
Having private constructor will not allow anyone outside of your class to create new object. This concept is used in Singleton pattern or class.
class Animal() { // Private Constructor private Animal(){ System.out.println(" This is coming from Animal Class."); } }
Constructor with Arguments
Argument-ed constructor are used to initialize instance variables in any class. Lets take an example here
class Dog(){ private Collar collar; public Dog(Collar collar){ this.collar = collar; } }In this example we want Dog object to have a collar. Every time someone need object of Dog, he can choose to supply object of Collar.
Note
In this case you JVM will not create default public constructor for you. So if you are inheriting Dog then you need to call this constructor by yourself.super in Java
super is a keyword in java and it is used to call constructor of parent class.
Example
class Dog(){ private Collar collar; public Dog(Collar collar){ this.collar = collar; } } class DomesticDog extends Dog(){ public DomesticDog(){ super(new Coller()); } }
In this example Domestic Dog is inheriting Dog and Dog has parameterized constructor. So for this super() needs to be invoked from constructor of DomesticDog class. You can pass the object of Collar.
0 Comments
Post a Comment