Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What's the difference between Inheritance and Polymorphism?

Parent

What's the difference between Inheritance and Polymorphism?

+5
−4
I'm using Java code as an example, but this can be answered if you don't know Java.
class Bird{
      public void sing(){
          System.out.println("Testing");
      }
}

class anClass extends Bird{
      public void sing(){    //If I remove the method than, `c.sing()` in main method (Polymorphism class) will print,"Testing"
          System.out.println("Testing second time");
      }
}

public class Polymorphism{
     public static void main(String[] args){
         Bird b = new Bird();
         b.sing();    //will print,"Testing"
         anClass c = new anClass();
         c.sing();   //will print,"Testing second time"
     }
}
class numberOne{
      public int add(int a, int b){
          return a+b;
      }
}

class numberTwo extends numberOne{
      public int sub(int a, int b){
          return a-b;
      }
}

public class Inheritance{
     public static void main(String[] args){
         numberTwo b = new numberTwo();
         b.add(5,3);    //will print,"8"
         c.sub(10,4);   //will print,"6"
     }
}

I had studied OOP, inheritance and polymorphism before, but I could never really get it.

In the examples above, I noticed that in the polymorphism examples, the methods don't return a value, while in the inheritance examples, they do. Would that be the difference between those two concepts?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

"returning values in Inheritance" ... What? o.O? (1 comment)
Post
+8
−0

Does Inteheritance actually meant return and Polymorphism meant to print out values?

This doesn't make the slightest sense... Probably you should forget all you've heard - don't "watch tutorials", start over with a good book on the subject written by an actual expert.

To answer the questions, inheritance means what it sounds like - that a class inherits some or all of the methods/members of the parent class.

Polymorphism means that you can have an instance of the inherited class, point to it with a base class pointer, call a method of the base class and then automatically get the derived method called instead.

Example with some generic pseudo-code below. Suppose you have a base class and an inherited class like this:

class Animal
{
  MakeNoise() { ... }
}

class Dog : public Animal // whatever the syntax for inheritance is in the given language
{
  MakeNoise() { print("Woof") }
}

Now suppose there is a function DoStuff(Animal obj) { obj.MakeNoise() }. If we pass a Dog object to this function, then polymorphism means that "Woof" gets printed even though whoever implemented DoStuff has no idea about the Dog class. When the default behavior gets implicitly replaced by derived behavior, without the need to rewrite the original code, then you have polymorphism.


What does OOP actually mean?

Object Oriented Programming is a way to design programs. There's obviously tonnes of study material about the topic already available, but to summarize it briefly, it roughly consists of 3 things:

  • Private encapsulation. Don't expose internals of a class outside that class. Mainly because none else needs to know about the internals. But also to prevent namespace collisions.

  • Creating autonomous classes with "loose coupling". The class should do it's designated task and only know and depend on the things needed for that task. It shouldn't know or access unrelated things. It should have as few dependencies as possible on other classes. This is to reduce bugs and complexity, and to make code easier to develop and maintain.

  • Inheritance. Designing base class templates that other classes can derive from. This is actually a far less important feature than the others - it is useful in some situations, but should be used with care since it is very hard to predict the future needs of a certain code base in advance.

Then there's various features that makes OO easier, such as constructors/destructors, RAII, member functions with a "this object" and so on. They aren't strictly speaking necessary for OO, but quite handy to have.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

You had explained Polymorphism briefly but, Inheritance? (2 comments)
You had explained Polymorphism briefly but, Inheritance?
deleted user wrote over 3 years ago

You just wrote a single line of Inheritance inheritance means what it sounds like - that a class inherits some or all of the methods/members of the parent class. I think(from your context),Inheritance is actually meant to extend a class to a parent class, isn't it? But, from your context Polymorphism is working like that also. Let we have two class A and B. B extends A than, we can call methods of A from B class also. Where A is parent class. Both sounds same to me.

Lundin‭ wrote over 3 years ago

@istiak Inheritance means an "is a" relation. "A dog is an animal". You can't have polymorphism without inheritance. They aren't mutually exclusive or something. Inheritance without polymorphism means that if the MakeNoise function got called as per my example, the function of the base class would be the one executed, not the function of the inherited class.