1

According to this Can an abstract class have a constructor? the constructor does not build the object and it is used to initialize fields so what is used to build the object? As in abstract class the constructor exists and it's not allowed to make an instance from that class and only could inherit it.
so the question is

What really builds the object in java ??

Zissouu
  • 934
  • 2
  • 10
  • 17
ahmed khattab
  • 1,778
  • 1
  • 9
  • 24
  • 2
    Possible duplicate of [Can an abstract class have a constructor?](https://stackoverflow.com/questions/260666/can-an-abstract-class-have-a-constructor) – Dorian Gray Sep 12 '17 at 17:13
  • An abstract class cannot be instantiated by definition; it's always a concrete class (anonymous or named) that gets created. – Mick Mnemonic Sep 12 '17 at 17:49

3 Answers3

3

An instance of an object in Java is created every time you use the new keyword. For example,

MyClass myObject1 = new MyClass("John", 1, 1000.20);
MyClass myObject2 = new MyClass("Mary", 2, 200);
MyClass myObject3 = new MyClass();

However, you can only instantiate a concrete class and never a abstract class. So a question arises, what's the point in having an abstract class?

You have a abstract class to provide a common set of functionalities which can be shared by all its subclasses, e.g., attributes, fields, etc.

And of course, an abstract can have a constructor. If you don't provide a constructor explicitly, it will have a default constructor. Whenever one of its concrete subclasses is instantiated, the constructor of the subclass will call the super constructor before doing any type of work in its own constructor. For example,

public abstract class MyAbstractClass {
   private String name;

   private int age;

   public MyAbstractClass() {
       name = "bozo";
       age = 5;
   }

  public MyAbstractClass(String name, int age) {
      this.name = name;
      this.age = age;
  }
  ...
}

public class MyClass extends MyAbstractClass {
    private double income;

    public MyClass() {
        //calls super() implicitly.
    }

    public MyClass(String name, int age, double income) {
        //calls super explicitly
        super(name, age);
        this.income = income;
    }
}
Indra Basak
  • 6,026
  • 1
  • 18
  • 37
2

Constructor is mandatory for any class.

All classes (abstract like concrete) have to declare a constructor.
It may be a generated no arg constructor or a explicitly declared constructor but anyway, the constructor presence is mandatory.
Why ?
Because for any class that inherits from a class (that is : all classes but Object), the first statement of any invoked constructor is invoking the direct parent constructor, that may at its turn invoke its parent constructor, and so on.

Use the new operator to create an instance

As you want to create an instance of a specific class, you have to invoke the constructor of its class by prefixing it with the new operator :

new MyObject();

But it makes sense only for concrete classes.

Indeed, abstract classes cannot be instantiated

So, the constructor of the abstract class will be used but only as you create an instance of a child class (whatever its level in the hierarchy) of this abstract class (what I explained in the first point).

Suppose Child is concrete and Parent is abstract, you have to invoke the parent constructor of Child in the Child constructor :

 Child(){super();}

But you will never invoke new Parent() as it is not legal to instantiate an abstract class.

davidxxx
  • 104,693
  • 13
  • 159
  • 179
1

The new Operator does build the Object indeed.

The reason why an abstract class always has a constructor (explicit or implicit) is because it is called from it's not abstract subclasses.

Dorian Gray
  • 2,591
  • 1
  • 7
  • 25