Object creation | Sololearn: Learn to code for FREE!


Object creation

If there is a super class named [Animal] and a child class named[Cat], then what is the difference between 1.Cat c=new Cat(); and 2.Animal c=new Cat() ; Pls explain.

4/26/2021 3:35:36 AM

M. Jawahirullah

2 Answers

New Answer


The first one is "INHERITANCE" and the second one shows the "POLYMORPHISM"


if Cat has methods miau() and Animal not, in second way c can't use this method (without casting) public class Program { public static void main(String[] args) { Cat c=new Cat(); c.miau(); Animal a=new Cat(); //a.miau(); } } class Animal {} class Cat extends Animal { void miau(){} }