What is the difference between coding a class object in its reference type and the parent reference type

I have three classes

public class Abc
{
    public methAbc()
    {
        System.out.println("parent class method");
    }
}


public class Xyz extends Abc
{
    public methAbc()
    {
        System.out.println("overriden parent class method");
    }

    public methXyz()
    {
        System.out.println("child class method");
    }
}

class Pqr
{
    Xyz childObjChildRef = new Xyz();
    Abc childObjParentdRef = new Xyz(); 
}

in the Pqr class what is the main difference in creating objects in different links

1: when holding an object in the same class reference ==>, then we can call all methods from the class

2: when holding an object in the link of the parent class ==>, then we can only call the overridden methods from the class

+3
source share
3 answers

In java, you are asked to use, if possible, a supertype or interface that your particular type implements. This separates your code from the actual implementation of the class.

: , . Enemy, (collide(), shoot(), decideNextMovement() ..). , , ; , Enemy (EnemyShip, EnemyMonster, EnemyParachuter ..), - .

- :

http://en.wikipedia.org/wiki/Open/closed_principle

+4

"childObjParentdRef" Xyz, , "methAbc", Xyz, Abc.

, "/" "-/" "" , , "" "" / ".

#.

+2

Xyz childObjChildRef = new Xyz(); Abc childObjParentdRef = new Xyz(); , Xyz . , , Child, . Xyz , , . , ,
, , .

+2

All Articles