views:

97

answers:

8
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?

A: 

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

super(name);
Macmade
Even if i remove Person constructor, default will be called.
mohit
+2  A: 

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
Then why Person object cannot be created explicilty, [violating what Java Spec says]
mohit
@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
@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
A: 

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
A: 

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
+1  A: 

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
A: 

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
+1  A: 

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
A: 

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