Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why we assign the reference of derived class object to the base class reference in java?

What may be the use of this assignment in the following program?
Java
class base4
{
    void fun()
    {
        System.out.println("Base function");
    }
}
class der2 extends base4
{
    void fun1()
    {
        System.out.println("Derived function");
                
    }
}
public class demo15 
{
    public static void main(String args[])
    {
        base4 b4=new base4();
        der2 d2=new der2();
        b4=d2;
        b4.fun();
    }
}
Posted
Updated 12-Dec-11 19:39pm
v3
Comments
NikulDarji 13-Dec-11 1:36am    
that is because base class does not know about the derived class attributes and methods.

Your example is not so good.

Let's try something different: Vehicles. You can assume that all Vehicles have an engine with a certain power:

public class Vehicle{

   private int iPower = 0;

   public Vehicle(int iPower){
       this.iPower = iPower;
   }

}


Now, there are different Vehicles on the road. e.g. some trucks, some cars and some tractors. All those Vehicles have an engine - but some might have additional features:

public class Truck extends Vehicle{

   private int iLoad = 0; // in tons

   public Truck(int iPower){
      super(iPower);
   }

   public Truck (int iPower, int iLoad){
      this(iPower); // calling 1 arg constructor
      this.iLoad = iLoad;
   }
}


public class Car extends Vehicle{

   private int iSeats = 1; // minimum

   public Car(int iPower){
      super(iPower);
   }

   public Car(int iPower, int iSeats){
     this(iPower);
     this.iSeats = iSeats;
   }
}


We have Vehicles as a base class, Trucks and Cars as more specified Vehicles.

Now here is the trick:

We will set up a Road and let Vehicles - no matter what kind of Vehicles those are - drive on it:

public class Road{

   public void drive(List<vehicles> oVehicles){
   // funny traffic action
   }

}</vehicles>


So the base class is a general approach to work with "the group" of those objects. We can cast the Vehicle to the more specified version if needed:

Truck oTruck = (Truck)oVehicles.get(0);


But that is just needed when we want to do special things. In general these are Vehicles - that's enough to let them use the road.

This system can be used in a lot of places:
A natural person e.g. is defined by gender, name and date of birth(or) place of birth.
A customer is a natural with a history of bought items.
An employee is also a natural person having other properties (right to enter the depot, can take cash).
A robber is a natural person with a gun.

But all of those natural persons are walking through the shop. Until...
 
Share this answer
 
v2
Comments
Nagy Vilmos 13-Dec-11 10:21am    
You missed that a Robber is a Person with a Weapon. Knife, Gun and Cosh all extend Weapon. :)

Seriously, good answer.
TorstenH. 13-Dec-11 10:48am    
thanks
just because base class does not know about the derived class attributes and methods......:)
 
Share this answer
 
Comments
Nagy Vilmos 13-Dec-11 10:56am    
Not a very useful answer really, especially considering TorstenH's above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900