Java Inheritance
Like the same way Objects of one class will inherit the some or complete features of its parent class or super class. In the java language also we have an a feature of inheritance as its developed based on the OOPs principles, to adopt the feature of the inheritance in java need to use the keyword extends.
What does Inheritance means, its nothing but acquiring something from someone(Can be from parents or anyone).
Example of Inheritance in java
class ParentA{ public String property="Parent Property"; } public class ChildA extends ParentA { public static void main(String[] args) { ChildA child =new ChildA(); System.out.println(child.property); } }
In java, private data members of the super class or parent class cannot be accessed directly although child class extends the parent class.
But we can access the data members by invoking the getter methods of the Parent Class.
Example of Accessing Private Data Members
class ParentA { private String property="Parent Property"; public String getProperty() { return property; } } public class ChildA extends ParentA { public static void main(String[] args) { ChildA child =new ChildA(); System.out.println(child.getProperty()); } }
Java doesn't support multiple inheritance due to the diamond problem.