aspose file tools
The moose likes Beginning Java and the fly likes how abstract class constructor invokes while creating instance of Subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "how abstract class constructor invokes while creating instance of Subclass" Watch "how abstract class constructor invokes while creating instance of Subclass" New topic
Author

how abstract class constructor invokes while creating instance of Subclass

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 361
Users not allowed to create instance of Abstract Class while We create the instance of subclass then
How the abstract class constructor calls.

I am having simple code:



child class having default constructor , how compiler creates default constructor for abstract class also.

It is because of what , while same code in interface not allowed.

Is it the nature of both different as Interface is pure Abstract class while Abstract class in partial. This is theoretically correct.

As i am want to know how internally it could be treated by JVM or design level.

We also know every class extends Object class.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

That code isn't creating an instance of an abstract class. And there's nothing unusual or mysterious about the fact that a subclass's constructor has to invoke a constructor of its superclass, even if the superclass does happen to be abstract. In fact nothing different happens when the superclass is abstract.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 361
I am not saying it is creating instance but calling the default constructor once we create the instance of subclass. right ?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yes because your subclass extends the abstract class. Every class (not interface, including abstract classes) has at least one constructor. In your case there is an implicit call to the superconstructor in base (which is added by the compiler).


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 361
yes you are right if we say about class means we have the constructor of that class like Abstract class
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

My point is, there are rules for how constructors work, including how they call constructors of their superclass. And those rules are the same whether any of the classes are abstract or not. In particular abstract classes need constructors just like any other classes. So the answer to your question is "It makes no difference whether the superclass is abstract or not."

It's possible that you don't understand the rules and need to discuss them here. That would be perfectly normal for Beginning Java. But abstract classes don't come into that question.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how abstract class constructor invokes while creating instance of Subclass
 
Similar Threads
why compile error
default constructors and constructors
abtract class object is not created
Constructor in interface?
Use of constructor in abstract class?