+ 1
the difference between inheritance and polymorphism?
Very confused pls help
6 Answers
+ 12
Inheritance: Think of a sheep... it inherits all it's attributes and behaviour from it's mummy and daddy sheep. The sheep just knows how to 'baaaa' naturally.
Polymorphism: Think about the big bad wolf who dresses up like a sheep... it pretends and acts like a sheep externally, but deep down the wolf is mimicking the behaviour that comes natural to the sheep.
From the outside, both appear to be sheep. on the inside, one is still just a big bad wolf...
@===~ âŹ%____
ă ă | | \ :)
+ 8
Inheritance is when one class gains the methods and properties of another class. Polymorphism is when one class has properties of another class, but depending on what class object you're calling the method from, different code is executed.
+ 2
Inheritance and polymorphism aren't apples to oranges or even apples to apples, inheritance lets you extend a base class and use its methods and properties in your derived class. This is done to ensure all derived classes have a base set of properties and methods. Polymorphism is a way of requiring the same methods and properties for all derived classes but allows you to write a custom method or property in place of the existing one.
Inheritance can be described almost like a framework for all derived classes. All classes that derive from a Vehicle class will have base properties and functions like Weight, Type, TopSpeed, Accelerate, Stop, Power, etc. A car and a motorcycle would both derive from the Vehicle class but both have different engine types and wheel counts and placement so the Accelerate method wouldn't work the same for both. Polymorphism would allow you to inherit the properties of the vehicle class but re-write the accelerate method to work as needed for the specific vehicle while also still ensuring that any derived class will always have an accelerate method no matter how it works.
+ 1
INHERITANCE :
okay...Julio
you have parents (father &mother) some things of you it's inherited from them like (tall,hair, size,eye......ect)all of those attributes become from them...
POLYMORPHISM :
then set that your father is a doctor and your mother is teacher(jobs) ... but you don't want to be doctor or teacher .... you want to PROGRAMMER.... yes it's okay and you can.... just get the (jobs )from your parents and adjust it to be PROGRAMMER...
NOTICE :
you can't adjusting any thing until you make inheritance...
0
thanks guys I actually posted a code called "awesome polymorphism" and I realized that I dont know the difference between them đ
- 1
Class Julio{
public int Money = 9999; //Inheritable
public int Money { get; set; }
public virtual void JuliosSalary(){ console.write("8888"); } //Inheritable but useless.
}
Class JulioSon : Julio{
public override void JuliosSalary(){ console.write("7777"); } //Your son has his own salary.
}
// Your son has access to Money : Inheritance
// Your son has access to JuliosSalary. But, his salary is not equals to yours. Thus, polymorphism is needed for adjustments.