Reputation: 3092
package sample;
A very simple question in Java, When I am trying to do like
Parent parent = new Child(); // Works
but the other way is not possible. Like Child child = new Parent();
Why?
Secondly, what is the difference between instantiating child like
Child c = new Child(); vs Parent p = new Child();
When should I use what?
Code snippet below:
public class A{
public void display(){
System.out.println("hello");
}
}
class B extends A {
public void display(){
System.out.println("hi");
}
public static void main(String args[]) {
A a = new B();//works
a.display();
B b = new A();// doesnt work
}
}
Upvotes: 0
Views: 980
Reputation: 25372
Firstly, you must understand what subclassing itself is.
If I have an object, let's say Tree
, I may give that object a list of methods like plant()
, water()
.
Then I want to define another class, AppleTree
. Well an apple tree still needs to be planted and watered, so instead of copying that code from the Tree
class, I'll just subclass it. So AppleTree extends Tree
.
This means I can now call Tree
's methods directly on an AppleTree
object. Then maybe I'll add another method to AppleTree
called harvest()
.
Now let's use this in another class. Maybe I have a class called Orchard that contains all my trees. Let's say I have 10 trees, and 2 of them are AppleTrees
. In my orchard, I want to water all of my trees.
I can store all of my trees in an ArrayList
like so:
ArrayList<Tree> trees = new ArrayList<Tree>();
for(int i = 0; i < 8; i++)
{
Tree t = new Tree();
trees.add(t);
}
for(int i = 0; i < 2; i++)
{
AppleTree a = new AppleTree();
trees.add(a);
}
I can then iterate through each of my trees in my ArrayList
and call water()
on each one.
for(int i = 0; i < trees.size(); i++)
{
trees.get(i).water();
}
However, now I want to harvest all of my trees, but only 2 of them are AppleTrees
and know a harvest()
method. So I can't do this:
for(int i = 0; i < trees.size(); i++)
{
trees.get(i).harvest(); //compiler error
}
This is because my ArrayList
holds a set of Tree pointers, which don't know a harvest()
method. Not all of my Trees
are AppleTrees
, and the class signature of Tree
does not match the class signature of AppleTree
.
All AppleTree
objects are Tree
objects, so this is ok:
Tree t = new AppleTree();
However, not all Trees
are AppleTrees
, so this doesn't work:
AppleTree a = new Tree(); //error
It's important to note though, that if I define a Tree
this way:
Tree t = new AppleTree();
Even though t
contains an AppleTree
object, I can't call t.harvest()
without casting t
to an AppleTree
, because to the compiler, it's just a Tree
and doesn't know the harvest()
method.
So this will fail:
Tree t = new AppleTree();
t.harvest(); //error
Upvotes: 2
Reputation: 590
Parent parent = new Child();
works because this is inheritance, bigger container (parent type reference variable) can accommodate child object but vise versa is not possible. All properties(methods) of parent are bound to be present in child as part of inheritance so
Parent parent = new Child(); // works
but child can have additional things(methods/properties) not present in parent and that's why
Child child = new Parent(); // doesn't work
Child c = new Child(); vs Parent p = new Child();
this is part of polymorphism here's an example to explain, when Pet pet = new Dog(); and we call pet.makeSound() vrs Dog dog = new Dog(); and we call dog.makeSound()
import java.util.ArrayList;
import java.util.List;
abstract class Pet{
public abstract void makeSound();
}
class Cat extends Pet{
@Override
public void makeSound() {
System.out.println("Meow");
}
}
class Dog extends Pet{
@Override
public void makeSound() {
System.out.println("Woof");
}
}
public class PolymorphismDemo{
public static void main(String args[]) {
//Now Pet will show How Polymorphism work in Java
List<Pet> pets = new ArrayList<Pet>();
pets.add(new Cat());
pets.add(new Dog());
//pet variable which is type of Pet behave different based
//upon whether pet is Cat or Dog
for(Pet pet : pets){
pet.makeSound();
}
}
}
Output: Meow Woof
Upvotes: 2