BIG
DATA

JAVA

JAVA ARTICLE

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

Difference between Child c = new Child(); and Parent p =new Child();

Sunday, Mar 06, 2016


Check at the code below:

class Parent {
	String name = "Vivek";
}
class Child extends Parent{
	int age = 30;
}
class Demo {    
    public static void main (String [] args) {
    	Child c = new Child();
    	System.out.println(c.name);
    	System.out.println(c.age);
    	Parent p = new Child();
    	System.out.println(p.name);
    	//System.out.println(p.age); compile error
    }
}

Output:

Vivek
30
Vivek

If you have gone through the up-casting and down-casting article by now you would have had a clear picture why compile time error when object p is trying to access age.
When you assign like this:

Child c = new Child();

Object 'c' has access to child's properties as well as its parent class properties. Hence accessing c.name and c.age was possible. By using child reference we can call both parent and child class (state)Fields and (behavior)methods.
And when you assign like this:

Parent p = new Child();

It is called as up-casting. Object 'p' has limited access. It can only access parent class properties, because p is a refernce to parent class. Hence accessing 'p.age' was not possible eventhough p is a child object. So by using parent reference we can call only methods available in parent class and child specific methods are not accessible.

If that is the case what is the advantages of Parent p = new Child();
It is nothing but polymorphism. If we don't know exact runtime type of object then we should use this approach(polymorhism).
Well we saw disadvantage of this approach, i.e child's properties is not accessible. But the advantage is:
Parent reference can hold any child class object. Imagine the below scenario:

class Parent {
	void work(){
		System.out.println("working");
	}
}
class Child1 extends Parent{
	void relax(){
		System.out.println("relaxing 1");
	}
}
class Child2 extends Parent{
	void relax(){
		System.out.println("relaxing 2");
	}
}
class Child3 extends Parent{
	void relax(){
		System.out.println("relaxing 3");
	}
}
class Child4 extends Parent{
	void relax(){
		System.out.println("relaxing 4");
	}
}
public class Demo {    
    public static void main (String [] args) {
    	List<Parent> lst = new ArrayList<Parent>();
    	lst.add(new Child1());
    	lst.add(new Child2());
    	lst.add(new Child3());
    	lst.add(new Child4());
    	for(Parent p:lst){    		
    		p.work();
    	}
    	Child1 c1 = (Child1)lst.get(0);
    	c1.relax();
    	Parent parent = getChild("2");
    	parent.work();
    }
    public static Parent getChild(String childName){
    	Parent p = null;
    	if(childName.equals("1")){
    		p = new Child1();
    	} else if (childName.equals("2")){
    		p = new Child2();
    	} .......
    	return p;
    }
}

Output:

working
working
working
working
relaxing 1
working

These are few of the usages of up-casting and down-casting in java. Up-casting and down-casting are great way of implementing polymorphism in your design.

Contribution

Sincere thanks from CoreJavaGuru to the author for submitting the above article.


LinkedIn