• Post Reply Bookmark Topic Watch Topic
  • New Topic

Main() and Constroctor inherited?  RSS feed

 
Ransika deSilva
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.
 
Timmy Marks
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.
 
Dirk Schreckmann
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!