public class ClassA { public void MethodA() { System.out.println("This is Method A"); } } public class ClassB extends ClassA { public void MethodB() { System.out.println("I am Method in Class B"); } } public class ClassC { public static void main(String[] args) { ClassA objClassA1 = new ClassA(); ClassB objClassB2 = new ClassB(); //Child Class of Parent Type can be Created ClassA objClassB1 = new ClassB(); //Assigning a Parent class Type to Child Class is Not Allowed //Casting Should be Carried out ClassB objClassA2 = (ClassB) new ClassA(); objClassA1.MethodA(); objClassB2.MethodA(); objClassB2.MethodB(); objClassB1.MethodA(); } }
You must be logged in to post a comment.