Understanding Java Inheritance

 Java Inheritance



In OOP concept, there are 4 main principles. They are,
  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism
In this blog we discuss about Inheritance.

    We can simply introduce the process of relating one class to another class as inheritance. Inheritance means creating new classes based on existing ones. In inheritance one class can inherit the properties and behaviors of another class.

    In Java, a superclass (base/parent class) is a class that is extended or inherited by another class. The class that inherits from the superclass is called a subclass (derived/child class).

    The members (attributes and methods) of the superclass should have appropriate access modifiers (e.g., public, protected) to control their visibility in the subclass.

    Things of the parent class are inherited by the child class, but the private things of the parent class are not inherited by the child class. However, things in the child class are not inherited by the parent class.





    Although we have created 2 classes, the contents of the parent class are not automatically inherited by the child class.. We need to use a keyword for that. We use extends keyword for it.
    

    If we have same methods , same attributes in parent and child class, we no need to add them in both classes. We can add them only in parent class. Because child class can access parent class things.



    

Comments

Popular posts from this blog

අද අපි කතා කරමු Data collections ගැන

Understanding and Mastering Coding Errors

OOP key points