Click here to Skip to main content
14,924,456 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am having a abstract class 'AbsBase' having a method getDetails and there is derived class 'DerivedBase' which is implementing getDetails method of AbsBase as shown below......
C#
public abstract class AbsBase
{
   public abstract void getDetails();
} 

public class DerivedBase : AbsBase
{
   public override void getDetails()
     {
        //Implementation code
     }
}

C#
AbsBase obj =new DerivedBase();

so what is 'obj' here of 'AbsBase' or what we call to obj here....

What I have tried:

Abstract Class cannot be instantiate.......
Posted
Updated 24-Aug-20 3:38am
v2
Comments
PIEBALDconsult 24-Aug-20 11:00am
   
Are you saying that that code won't compile? It should.

1 solution

Abstract classes can;t be instantiated, no - that's the whole point. They are a "generic base class" from which concrete examples are derived.
For example, a Car class would be abstract, and give rise to derived concrete classes FordFiesta1300, MercedesA180CDi, ToyotaHiLux, and so on - because you don't buy "a car", you buy "a Ford Fiesta 1300", or "a Mercedes A250e", or ...
Car describes all the thigs that make it "a car" : engine, steering wheel, drivers seat, four wheels, and so on; while MercedesA180CDi fills in the details: "2L Turbo Diesel", "W169 body shape", and so on. The actual instance of a car "my car" adds teh details which differenciate it from all other A180CDis: colour, VIN number, registration number, registered owner, current location, current speed, fuel in tank level, and so on.

In your example:
C#
AbsBase obj =new DerivedBase();

obj is of the type AbsBase, which means it can contain an instance of any class derived from AbsBase.
In Car terms:
C#
Car myCar = new MercedesA180CDi(Color.Black, "WDD169000J000000, "XX 99 XXX", originalGriff);
myCar can hold any type of Car - a ford, a Mercedes, a Citroen, ... because all such vehicles are derived from the abstract base class Car.

The system sorts out from the individual instance which method it shoudl call when you try to access a base class method:
C#
myCar.Drive();

Or
C#
obj.getDetails();


Make sense?
   

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