Type Casting in Inheritance

As you know inheritance in Java is a very important topic in object-oriented programming. So, here we will discuss some important inheritance rule. Here we will discuss the typecasting of the object. These are the important inheritance rule that should be discussed.

Case 1: Parent class reference can hold the child class object but vice versa is not possible.

class Parent
{
	String name = "Ram";
	public void printData()
	{
		System.out.println("Name of person = "+ name);
	}
}

class Child extends Parent
{
	int age = 25;
	public void showData()
	{
		System.out.println("Age of person = "+ age);
	}
}

class MainClass 
{
	public static void main (String [] args) 
	{
		Parent parent = new Child();
		parent.printData();
	}
}

Output: Name of person = Ram

Let’s try what will happen if Child class reference holds the Object of a parent class.

class Parent
{
	String name = "Ram";
	public void printData()
	{
		System.out.println("Name of person = "+ name);
	}
}

class Child extends Parent
{
	int age = 25;
	public void showData()
	{
		System.out.println("Age of person = "+ age);
	}
}

class MainClass 
{
	public static void main (String [] args) 
	{
		Child c = (Child) new Parent();
	}
}

Output: Exception in thread “main” java.lang.ClassCastException: Parent cannot be cast to Child at MainClass.main(MainClass.java:23)

Important points about inheritance in java

Case 2: The reference of parent class can’t access the variables and functions of a child class. If you try to access the method of child class by Parent’s class object, then the compiler will show an error.

class Parent
{
	String name = "Ram";
	public void printData()
	{
		System.out.println("Name of person = "+ name);
	}
}

class Child extends Parent
{
	int age = 25;
	public void showData()
	{
		System.out.println("Age of person = "+ age);
	}
}

class MainClass 
{
	public static void main (String [] args) 
	{
		Parent parent = new Child();
		parent.showData();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem:     The method showData() is undefined for the type Parent at MainClass.main(MainClass.java:24)

parent.showData() will show compilation error. Because showData() is defined in Child class.

The solution to the problem: You can create the reference and object of Child class. Because Child class inherits all the features of Parent class. So, the object of a child class can access the variables and methods of both classes. It is most common and important points about inheritance in java.

class Parent
{
	String name = "Ram";
	public void printData()
	{
		System.out.println("Name of person = "+ name);
	}
}

class Child extends Parent
{
	int age = 25;
	public void showData()
	{
		System.out.println("Age of person = "+ age);
	}
}

class MainClass 
{
	public static void main (String [] args) 
	{
		Child child = new Child();
                child.printData();
                child.showData();
	}
}

Output:
Name of person = Ram
Age of person = 25

Leave a Comment