Important Points for Inheritance in Java

躍然發表於2015-07-06

1.Private members of superclass are not directly accessible to subclass. As in this example, Animal variable noOfLegs is not accessible to Cat class but it can be indirectly accessible via getter and setter methods.
2.Superclass members with default access is accessible to subclass ONLY if they are in same package.
3.Superclass constructors are not inherited by subclass.
4.If superclass doesn’t have default constructor, then subclass also needs to have an explicit constructor defined. Else it will throw compile time exception.
5.Java doesn’t support multiple inheritance, a subclass can extends only one class. So here Animal is implicitly extending Object class and Cat is extending Animal class but due to java inheritance transitive nature, Cat class also extends Object class.

摘自:http://www.journaldev.com/644/inheritance-in-java-example

相關文章