inheritance implies that simple definition of inherit attributes and method from one class to another.
Above Picture explains A Class which is Super Class(Parent Class) B Class which is Sub Class(Child Class)
Arrow is indicate as inheritance. When you draw inheritance arrow draw from class B to A class because B class in defend on the Class A.B class is created using A class.
Create a Super Class as Animal.
Animal.java
public class Animal { public String leg = "leg"; //create instance variables public String mouth = "mouth"; public void Bark() { System.out.println("Bark........."); } public void eat() { System.out.println("eat"); } public void sleep() { System.out.println("Sleep.........."); } }
Create a Sub Class as Dog.
Dog.java
public class Dog extends Animal { public void dogMethod() { Bark(); eat(); sleep(); System.out.println(leg); System.out.println(mouth); } }
Main.java
public class Main { public static void main(String[] args) { //Create the Sub Object and Access the SuperClass and Subclass methods and attributes Dog dog = new Dog(); dog.dogMethod(); dog.eat(); dog.sleep(); //SuperClass reference Subclass object can create if you create like this you can access only Super class methods and attributes only //Subclass you cannot access Animal animal = new Dog(); animal.eat(); animal.Bark(); dog.sleep(); } }