Skip to content

Understanding Java OOP inheritance, private-protected access and type casting

Notifications You must be signed in to change notification settings

ashahrior/Breaking-Inheritance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Understanding Java OOP inheritance, private-protected access and type casting

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages