You can create Class B object with reference type to Class A beacause Class A is the supertype. But you can't create a Class A object with Class B reference type because Class B is a sub class of Class A and cannot assign a supertype object to an sub type
referenece. Your second code is invalid.
neha0123
Member
50 Points
224 Posts
C# class inherit problem
Jun 14, 2010 09:40 AM|LINK
there is a two class Class A and Class B, class b inherit class A ,
and we create like this
class A obj=new classB();
what is use of create instance of Classb,what is this,what do, plz brief me
class B obj=new classA();
this gives error.
can not implictity convert type. why error comes....
Ruchira
All-Star
42884 Points
7019 Posts
MVP
Re: C# class inherit problem
Jun 14, 2010 10:21 AM|LINK
You can create Class B object with reference type to Class A beacause Class A is the supertype. But you can't create a Class A object with Class B reference type because Class B is a sub class of Class A and cannot assign a supertype object to an sub type referenece. Your second code is invalid.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.rahul.kansar...
Member
524 Points
87 Posts
Re: C# class inherit problem
Jun 14, 2010 10:26 AM|LINK
Hi Neha
class B obj = new class A() throws error as it is not possible to create object of type base class with child class type...
First case is possible because as it is possibel to creat object of child class with base class.
Rahul
Please mark this reply as answer if it helps
getchinna_sv
Star
12043 Points
2096 Posts
Re: C# class inherit problem
Jun 14, 2010 10:32 AM|LINK
Let me give you one simple example...
class Employee{
}
class Manager : Employee{
}
class TeamLeader : Employee{
}
class Programmers : Employee{
}
Employee obj = new Manager() // this is valid... because all managers are employees in a company
Manager obj = new Employee() // this is not valid ... because all employees are not managers in a company
what you are doing here.. is you are trying to assign Base Class instance to derived class which is wrong...
<div></div>
neha0123
Member
50 Points
224 Posts
Re: C# class inherit problem
Jun 14, 2010 10:36 AM|LINK
what is the use of this....
getchinna_sv
Star
12043 Points
2096 Posts
Re: C# class inherit problem
Jun 14, 2010 10:42 AM|LINK
1. No need to write code from scratch. You can start coding with existing classes.
2. Through inheritance you can very easily convert small system into large systems
3. You can lots of classes with the same root but have different implementations of classes.
4. Code reusablity through inheritance increased.
5. Good at representing the objects
6. Inheritance provide a clear model structure which is easy to understand without much
complexity.
7. code are easy to manage and divided into parent and child classes.
http://www.expresscomputeronline.com/20021118/techspace2.shtml
neha0123
Member
50 Points
224 Posts
Re: C# class inherit problem
Jun 14, 2010 10:50 AM|LINK
i know all this i am asking
classA objclass=new classb();
classB is inherited from classA
getchinna_sv
Star
12043 Points
2096 Posts
Re: C# class inherit problem
Jun 14, 2010 11:08 AM|LINK
check this article... http://www.c-sharpcorner.com/UploadFile/pcurnow/polymorphcasting06222007131659PM/polymorphcasting.aspx
deeprajd
Member
470 Points
70 Posts
Re: C# class inherit problem
Jun 14, 2010 11:45 AM|LINK
In the case of live implementation this approach is really useful.
Let me explain with an example.
Say you have three class - Vehicle, Benz , BMW and the relation is like this
Benz and BMW class inherits Vehicle class.
// super class or the base class
class Vehicle
{
}
// child class or the derived class
class Benz : Vehicle
{
}
// child class or the derived class
class BMW : Vehicle
{
}
Now in the base class (super class) Vehicle, we are having a method Features which is virtual.
So our base class implementation is like this
class Vehicle
{
// return data type is based on logic - may be built-in or custom type
internal virtual <return-data-type> Features()
{
// Inside, you are specifying features specific to all vehicles
}
}
Also the child class or derived class - Benz and BMW has the same method overloaded
Now the child classes are like
class Benz : Vehicle
{
internal override <return-data-type> Features()
{
// Inside, you are specifying the features that are specific to Benz
}
}
class BMW : Vehicle
{
internal override <return-data-type> Features()
{
// Inside, you are specifying the features that are specific to BMW
}
}
Now we create a class which has a following method that is exposed (exposed means ,
this is the only method that is accessed by other classes that is outside the current assembly which contains
the above said classes.) to other classes outside the current assembly.
// Signature of the method that is exposed is as follows
public <return-type> GetVehicleFeatures(Vehicle objVehicle)
{
return objVehicle.Features();
}
The benifit of using like this is, in future if there is any other vehicle class we need to add, then
we can use the same method to access the new class method.
If you want an example from real world, Sea is s super class or base class and River is the sub class or derived class.
River merges into sea but vise versa is not possible .... :)
Hope this helps you( especially my real world example :) )
Regards,
Deepraj D
jawahars
Member
152 Points
103 Posts
Re: C# class inherit problem
Jun 16, 2010 11:34 AM|LINK
http://msdn.microsoft.com/en-us/library/ms173152(VS.90).aspx
object oriented programming
Thanks,
Jawahar
Lets screw the Technolgies.