Mam taki o to kod i wykorzystuje sie w nim fakt ze tworzy sie referencje do obiektu klasy abstrakcyjnej ( 1) a nastepnie przypisuje sie obiekty z podklas ( 2 ) i ( 3 ) . I moje pytanie co nam to daje skoro taki sam wynik dostanie postepujac normalnie ( 4 ) i ( 5)
package test;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
abstract class Figure
{
double dim1,dim2;
Figure(int a, int b)
{
dim1=a;
dim2=b;
}
abstract double area();
}
class Rectangle extends Figure
{
Rectangle(int a,int b)
{
super(a,b);
}
double area()
{
System.out.println("Pole prostokata wynosi ");
return dim1*dim2;
}
}
class Triangle extends Figure
{
Triangle(int a,int b)
{
super(a,b);
}
double area()
{
System.out.println("Pole trojkata wynosi ");
return (dim1*dim2) / 2;
}
}
public class test
{
public static void main(String args[])
{
Rectangle r = new Rectangle(9,5);
Triangle t = new Triangle(10,8);
Figure figref;
figref = r; // (1)
System.out.println(figref.area()); // (2)
figref = t;
System.out.println(figref.area()); // (3)
System.out.println(r.area()); // (4) < -- dostane ten sam wynik co w 2
System.out.println(t.area()); // (5) <-- dostane ten sam wynik co w 3
}
}