0
public abstract class Person {

    private String name;

    public Person(String name) {
        this.name = name;
        System.out.println("Person");
    }

    public String getName() {
        return name;
    }

    abstract public String getDescription();    
}


public class Student extends Person {

    private String major;

    public Student(String name, String major) {
        super(name);
        this.major = major;
    }

    public String getMajor() {
        return major;
    }

    @Override
    public String getDescription() {
        return "student" + super.getName() + " having" + major;
    }

}

public class PersonTest {

    public static void main(String[] args) {

        Person person = new Student("XYZ", "ABC");
        System.out.println(person.getDescription());

    }

}

Ques: We cannot create objects of abstract classes, then why Person Constructor has been invoked, even its an abstract class?

Alberto Zaccagni
  • 28,473
  • 10
  • 71
  • 102
mohit
  • 883
  • 2
  • 14
  • 25
  • 1
    Related: http://stackoverflow.com/questions/2623180/abstract-class-and-constructor, http://stackoverflow.com/questions/2105783/what-is-the-use-of-creating-a-constructor-for-an-abstract-class-in-java, http://stackoverflow.com/questions/260666/abstract-class-constructor-in-java – finnw Apr 21 '10 at 09:19
  • because its an abstract class there can be some private fields that may need to be intialized. – urmalp Apr 21 '10 at 11:08

8 Answers8

2

Because it's still a class and its constructor is invoked as a part of the object instantiation. The fact that it is abstract doesn't have anything to do with this.

Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
  • Then why Person object cannot be created explicilty, [violating what Java Spec says] – mohit Apr 21 '10 at 09:18
  • 1
    @mohit: because it is `abstract`, you can't invoke it **directly**, but you can still call it using `super(name);`, as you did. – Alberto Zaccagni Apr 21 '10 at 09:23
  • 1
    @mohit: if we were able to instantiate Person, what whould you expect getDescription() of the new instance to return? The whole idea is that Person is some kind of "template" with blanks in it, to be implemented by subclasses. – Eyal Schneider Apr 21 '10 at 09:26
1

What you can't do is to create an instance of an abstract class. As a Student is 'partly' a Person super(...) initializes the 'Person part' of the student, it does not create a Person.

I hope you understand what I try to say

maid450
  • 7,338
  • 3
  • 33
  • 32
1

Ques: We cannot create objects of abstract classes, then why Person Constructor has been invoked, even its an abstract class?

If a class is declared abstract, no objects of that class can be created. That DOESNOT mean you cannot create objects of its subclasses.

You can have references(of type abstract class) refer to a subclass(non abstract) object.

Person person = new Student("XYZ", "ABC");

And in order to construct a Student object, you need to have the "person" parts of the student initialized, thats what exactly the constructor of the abstract super class is called for.

Zaki
  • 6,669
  • 6
  • 32
  • 50
0

The constructor is just a method like others. And you are calling it explicitly from your child class' constructor with:

super(name);
Macmade
  • 49,018
  • 12
  • 104
  • 121
0

The Person constructor is invoked from the Student class. From the Java tutorials

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

Carlos Tasada
  • 4,388
  • 1
  • 18
  • 26
0

An abstract class can have constructors - they cannot be invoked directly, but only as part of constructing a subclass instance, via a call to super() in the subclass constructor.

Michael Borgwardt
  • 327,225
  • 74
  • 458
  • 699
0

One cannot make an instance of an abstract class but subclasses can call super(name);. Even though it is a constructor, it is just another method.

fastcodejava
  • 35,219
  • 24
  • 124
  • 181
0

The Abstract class is a part of your overall concrete class. An Abstract class is a class that is allowed to defer many parts to its concrete implementations but it must still initialize itself.

As such it has a constructor and as such the constructor with any parameters it requires to set itself up.

When you call the super(...) method from your Student class you are explicitly calling the constructor in the Person template class. The super() call must be in the first line of the Person constructor so if your person class wishes to override the defaults set up by the Person() constructor, then you have that option. But exactly one person constructor MUST be invoked when you extend a class (Abstract or concrete).

Chris
  • 4,200
  • 3
  • 35
  • 46