Why should I refer to the base class when I can access all methods in the same way, referring to the subclass?

I am learning Java concepts. I have doubts about the concept of Java inheritance. In inheritance, we can assign an instance of a subclass to a reference to the base class, and with this we can only access the function of the base class. and we can assign any instance of a subclass in the inheritance hierarchy to the base class. For the type of the instance that assigns a reference to a specific base class, we can only access the functions of the base class, and I did not find the difference.

Can someone give me a real concept of why we should designate subclass instances to reference base classes? what is the need? Instead, we can access these functions of the base class from the directory of the subclass, but only know.

Explain by looking at a specific base class and many subclasses in a hierarchy.

+3
source share
6 answers

The reason you can do this is to create more robust designs. Take, for example, the Java Framework collection. You have a List interface, and you have two implementations: ArrayList and LinkedList.

You can write your program specifically for using LinkedList or specifically for ArrayList. However, your program depends on these specific implementations.

, , List, List. , , - , :

  public void doSomething(ArrayList a){}

ArrayList, LinkedList. , LinkedList? ? .

  public void doSomething(List l){}

.

- , . List .

.

+5

- :

  • ,
  • , ,
  • ,

.

, , , , . , , Collection. , , isEmpty, contains size, .

+3

, , . " ".

, , , , , :

public interface Shape
{
    double calculateArea();
}

class Circle implements Shape
{
    private double radius;

    Circle(double r) { this.radius = r; }

    public double calculateArea() { return Math.PI*radius*radius; }
}

class Square implements Shape
{
    private double side;

    Square(double s) { this.side = s; }

    public double calculateArea() { return side*side; }
}

// This would be a separate JUnit or TestNG annotated test.
public class ShapeTest
{
    @Test
    public void testCalculateArea()
    {
        Map<Shape, Double> expected = new HashMap<Shape, Double>()
        {{
            put(new Circle(1.0), Math.PI);
            put(new Square(1.0), 1.0);            
        }};


        for (Shape shape : expected.keySet())
        {
            Assert.assertEquals(expected.get(shape), shape.calculateArea());
        }       
    }
}
+2

Polymorphism.

, List<String>. , , , , , , , .

, , , .. . LinkedList<String>, a ArrayList<String> - . , - , .

, , , , . , , .

+1

, - base, Car and Plane - . , move().

, . .

move() Vehicle?

(). move() Vehicle, , .. . , , , . , runVehicle (...), Vehicle.

void runVehicle(Vehicle v)
{
  v.move();
}

Car c=new Car();
runVehicle(c);

Plane p=new Plane();
runPlane(p);
+1

There is no real need for this, unless the API requires it. For example, if a specific API or code library has

void ReallyUsefulFunction(BaseClass instance)

which you would like to use, you can get the fom BaseClass class and implement its methods in SubClass. Then you can pass a subclass to the function.

However, technically you can implement your own

void MyReallyUsefulFunction(MyClass instance)

which mimics the same functionality. But, as MYYM explained, the benefits of code reuse, etc. They can be huge, and that's when you want to use polymorphism.

0
source

Source: https://habr.com/ru/post/1784264/


All Articles