Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Main() and Constroctor inherited?  RSS feed

 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Could you please explain these two concepts.
Main methods are inherited and can be overriden but the constructors are not inherited so can not override.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of it this way, you cannot override a constructor in the child class because you would need to provide a return type for the method (even if it is void), so you wouldn't be able to write a method with exactly the same signature.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are not members, nor are they methods, in Java.
Only members, such as variables or methods, can be inherited.
If something is not inherited, it cannot be overridden.
Perhaps another way to think of if something is inherited, is to consider whether it is directly accessible in the subclass.
Is anything becoming clearer?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!