-1

I tried to research over this topic but could not find a clarity over this. Please help me in this.

3
  • 5
    Without any dirty magics, you cannot create an instance of abstract class. Apr 18, 2017 at 7:56
  • 2
    You cannot instantiate an abstract class, but from a child-class, u can call the parent constructor with the super() method.
    – Hienz
    Apr 18, 2017 at 8:01
  • @Deepak luckily I know the dirty magic MyAbstract childObject= new MyChildClass();
    – 0xDEADBEEF
    Apr 18, 2017 at 8:06

4 Answers 4

3

In the constructor of a child-class you can call

super(/*params if needed*/);

in order to call its parent's constructor

0

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!

2
  • 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.
    – Deepak
    Apr 18, 2017 at 13:45
  • default and parameterized constructor both can be defined in abstract class Apr 18, 2017 at 13:56
0

Something like this :

abstract class MyClass{ 
    int val;
    public MyClass( int val) {
        this.val= val;
    }

}

class Test extends MyClass{
    public Test () {
        super(2);
    }
}
2
  • mir, I believe MyClass is a parametrized constructor within Abstract class.
    – Deepak
    Apr 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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.