• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why constructor didn't execute?

 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I learned for exam OCA 7.
Please help me: don't understand why constructor of class SubAlpha don't execute:



and 

Thank you
 
Hardik Nai
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am checked and founded class Chapter2_11 main called own constructor.
Rule of inheritance called super class constructor like Alpha.
SubAlpha class haven't any relationship  to  Chapter2_11.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Anda!!    As you are new, I've put code tags around your code, see It looks nice now with code tags. Please refer ( click here --->) Use Code Tags to know how to use code tag. Aways quote the source click here --->  Quote Your Sources
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:Please help me: don't understand why constructor of class SubAlpha don't execute:
When you create an object of a class, If that class extends another class then constructor of super class(If this superclass has another super ├žlass then constructor of super of this super class like that..) first gets executed then subclass's constructor.

In your example, Chapter2_11 extends Alpha so superclass of Chapter2_11 is Alpha but not SubApha (Note: a class can extend only one class). When you create an object of Chapter2_11 then first constructor of superclass of Chapter2_11 i.e. constructor of Alpha gets executed then constructor of Chapter2_11.

Chapter2_11 doesn't extend SubAlpha so constructor of SubAlpha doesn't execute.

Example 1:
Output:
Constructor of class One executed
Constructor of class Two executed
Constructor of class Three executed

In above example 1 you can see the constructor of superclass's gets executed first then subclass's ...
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hardik Nai wrote:I am checked and founded class Chapter2_11 main called own constructor.
Rule of inheritance called super class constructor like Alpha.
SubAlpha class haven't any relationship  to  Chapter2_11.


I realised the solution after you posted this question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic