简体   繁体   中英

Difference between inheritance & polymorphism

I am having issues understanding what exactly polymorhism is and how it differs from inheritance. I have researched the web and found many answers that gives very technical deffinitions on what polymorphism is but nothing in which i really understand too well. I have come up with an example that might be polymorphism but may not be.

MY example:

Say you have a Gym with many levels of membership.

  1. basic member
  2. silver member
  3. gold member

and each class is a basic member but has more function

class basicMember(){
private double rate = 10;
privatedbl poolPrice = 5;

public void setRate (dbl in);
{
 rate = in;
}
public dbl getRate()
{
 return rate;
}
public dbl getPoolPrice()
{
 return poolPrice;
}
} 

class silverMember extends basicRate()
{
 private dbl poolPriceDis = .9;
setRate(15);

public dbl getPoolPriceDis(){
return getPoolPrice() * poolPriceDis;
}
 } 

class goldMember extends basicRate(){
private dbl poolPriceDis = .85;
setRate(20);

public dbl getPoolPriceDis(){
return getPoolPrice() * poolPriceDis;
}
}

Would this be an example of inheritance or polymorphism or both? Please explain.....

Your example demonstrates inheritance.

  • Polymorphism lets you call methods of a class without knowing the exact type of the class. It deals with how the program decides which methods it should use, depending on what type of object it has. If you have a Animal, which has a eat method, and you have a Cow which extends Animal, which has its own implementation of Eat, which method gets called is depends if you have a pointer to Animal or a Cow.

Polymorphism allows you to write a method that works for any Animal:

public void pet(Animal animal) {
   ...
}

This method would accept Dog, Cat, etc, including subclasses of Animal that are yet to be written.

  • Inheritance lets derived classes share interfaces and code of their base classes. It is when a 'class' derives from an existing 'class'. So if you have a Animal class, then you have a Cow class that extends Animal, Cow inherits all the things that Animal has (depending on access modifiers).

    public class Animal {...}

    public class Dog extends Animal {...}

    Dog d = new Dog();

    d.DoAnimalStuff();

Inheritance in any language is a type of polymorphism. Polymorphism is the ability for multiple things (multiple classes, multiple functions, etc) to be treated in the same way. Inheretance allows that because you can treat any type as the base class. Another form of polymorphism in Java is generics- you can pass in any type, so long as it defines all the functions you need.

Inheritance is one object having the same logic as another object with some extra logic. Inheritance saves you lines of code.

Polymorphism is allowing multiple objects to be type-safely stored in the same place, and operated on with the same code

suppose d inherits/impliments from b

Base b = new Derived d;

b.DoThingsThatBasesCanDo();

Your example is just inheritance, but it can become polymorphism depending on your usage

It would be even more polymorphic if poolPriceDis was a member of your base class, and you either override it in your derived classes or initialized it in your respective class's constructers

Using Java...

Inheritance is the concept of defining a class that adds to, or extends, another class. It is also possible to change a class. For example, a boss might extend an employee by adding the ability to ask for a raise.

public class Employee
{
    ...
}

public class Boss extends Employee
{
    public void askForRaise(){}
}

Polymorphism, can be used a few ways. Lets look at two of them. First, different classes can do different things with the same method, for example, we can define a Shape class that represents all shapes and has a draw method.

public abstract class Shape
{
    public abstract void draw();
}

A square will implement draw to draw a square, while a circle will draw a circle. Both draw, but do different things, this is the first kind of polymorphism.

public class Square extends Shape
{
    public void draw(){...}
}

public class Circle extends Shape
{
    public void draw(){...}
}

It is worth noting that we also used inheritance in this example. Square and Circle inherit from Shape so they have all of the methods and properties that Shape has.

A second kind of polymorphism that shows up in Java is the ability to have the same method with different arguments. For example, a number might have an add method that takes an integer and another that takes a double:

public class MyNumber
{
    public void add(int i){...}
    public void add(double d){...}
}

Having two versions of the same method is another form of polymorphism.

Polymorphism comes in many shapes and sizes, one of which is inheritance.

Inheritance lets you use objects as if they were of the same class, where in reality they may be different classes. See your example code.

Overloading lets you use functions with parameters of different types as if they were the same. For example: add("hello", "world") and add(1, 2).

Parametrization lets you use functions or classes that don't care about the type, as long as you tell it in advance. For example: Vector<Float> versus Vector<String>.

In all cases you see this "different/many" (poly) acting as the "same/similar shape" (morph).

Quoting Mr. Cay Horstmann from Object Oriented Design & Patterns :

The ability to select the appropriate method for a particular object is called polymorphism . (The term "polymorphic" literally means "having multiple shapes".)

and

You use inheritance to model a relationship between classes in which one class represents a more general concept and another a more specialized concept.

In your case, you're just using inheritance. But if you would override, let's say, your setRate() method in the silverMember class and had something like this in the main:

basicMember member = new silverMember();
member.setRate(20);

then the setRate() of the silverMember class is invoked although you have a basicMember variable. That's polymorphism.

--

I wouldn't be true to myself if i did not advise you not to do this interview if you don't know this kind of stuff. Try reading the book i quoted in the beginning of the comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM