I tried to research over this topic but could not find a clarity over this. Please help me in this.
4 Answers
In the constructor of a child-class you can call
super(/*params if needed*/);
in order to call its parent's constructor
You can use super() in your child class. You cannot create instance of abstract class(you might know that)
abstract class Parent{
Parent(){
System.out.println("Parent contructor!");
}
}
public class Child extends Parent{
public Child() {
super();
System.out.println("Child contructor!");
}
public static void main(String[] args) {
new Child();
}
}
Even if you do not specify super()
in Child
class, still the parent class constructor will be called because super()
will be put by compiler in default constructor of Child
class while generating the .class. If you only have parameterized constructor in Parent class then you will have to explicitly call the parent class constructor by super(..parameter)
in your child class constructor . Hope this helps!
-
Sanket D, I am good with super() and the fact that instance of abstract class can't be created. But my confusion with respect to your answer is that 'Parent()' having print statement should be default constructor, so as per your example default constructor can be defined in an abstract class.– DeepakApr 18, 2017 at 13:45
-
default and parameterized constructor both can be defined in abstract class Apr 18, 2017 at 13:56
Something like this :
abstract class MyClass{
int val;
public MyClass( int val) {
this.val= val;
}
}
class Test extends MyClass{
public Test () {
super(2);
}
}
-
mir, I believe MyClass is a parametrized constructor within Abstract class.– DeepakApr 18, 2017 at 13:37
-
@Deepak there is no default constructor in the parent abstract class, the constructor used in subclass must explicitly call the parent constructor. Apr 18, 2017 at 13:58
While you may not call upon a default constructor for an Abastract class, you can invoke the default constructor for the sub-class that embodies your abstract class.
For example:
abstract class Animal {
public Animal (){ ... }
abstract public String sound();
}
class Horse extends Animal{
public Horse(){
super()
}
@Override
public String sound(){
return "neigh";
}
}
class Dog extends Animal{
public Dog(){
super()
}
@Override
public String sound(){
return "woof";
}
}
It is not directly possible to make a new Animal. You can make new Dog
-s and Horse
-s with annomyously for (common) Type Animal
example:
public Animal makeAnimals( Animal existingCreature )
throws InstantiationException, IllegalAccessException
{
return existingCreature.getClass.newInstance();
}
Will make a new instance of the concrete class you provided:
Dog fido = new Dog();
Horse hack = new Horse();
Dog puppy = makeAnimals( fido ); // new uninitialised Dog instance
Horse foal = makeAnimals( hack ); // new uninitialised Horse instance
Alternatively you can make an annomyous instance
Animal parrot = new Animal() {
@Override
public String sound(){
return "squawk";
}
}
As a way to make new animal types. If you were doing something for real like Animals it would probably be better to make a Parot
subclass.
MyAbstract childObject= new MyChildClass();