Java Hierarchical Inheritance Example


Program
class Bird{ //Parent Class String name3="Bird"; } class Crow extends Bird{ //Child Class 2 String name2="Crow"; } class Parrot extends Bird{ //Child Class1 String name1="Parrot"; } public class HierarchicalInheritance{ public static void main(String args[]){ Parrot p=new Parrot(); System.out.println(p.name1+" is "+p.name3); //System.out.println(p.name2+" is "+p.name3); this line will give error: cannot find symbol name2 } }
Input
Output