Java Inheritance Example


As in the above example, Parrot is SubClass i.e ChildClass and  Bird is SuperClass i.e ParentClass. The relationship between child class and parent class using extends keyword to show that Parrot is a type of Bird Class.

Here Parrot Class can use the field and method of own class as well as Bird Class.

Program
class Bird{ //Parent Class String name2="Bird"; void fly(){ System.out.println(" can Fly"); } } public class Parrot extends Bird{ //Child Class String name1="Parrot"; void color(){ System.out.print(" is a green color Bird. "); } public static void main(String args[]){ Parrot p=new Parrot(); System.out.print(p.name1); p.color(); System.out.print(p.name2); p.fly(); } }
Input
Output