Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 1.52 KB

README.md

File metadata and controls

24 lines (22 loc) · 1.52 KB

Breaking-Inheritance

I kept getting some confusion regarding inheritance and the private-protected access in Java OOP. I tweaked here and there in this code to see how things play out.

What I have come to understand-

  • A java source file can't include more than one public class
  • The source file name and the class containing the main() must be the same
  • A public class and the source file that contains it, both must have the same name
  • If there are multiple main() in different classes, then the class with the same name as its source file will be the driving main()
  • Private properties can't be directly accessed outside its class, even with its own class object. It can only be directly accessed withing its class boundary
  • Any parent class type variable can hold its child class type object. But not vice versa. Suppose if A is parent class and B is child class -
    • A obj = new B() - is valid
    • B obj = new A() - is invalid
  • Here obj can only have access to the properties of A. It wouldn't have access to any property in B
  • If there is some method in B that overrides one in A, then when obj calls that method then the one in B is invoked
  • Child class has direct access to its parent class's protected properties; actually has access to all its non-private properties
  • Child class object will override parent class's attributes of the same name

Do correct me if I am wrong.