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.

Post History

83%
+8 −0
Q&A What's the difference between Inheritance and Polymorphism?

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 tutoria...

posted 3y ago by Lundin‭

Answer
#1: Initial revision by user avatar Lundin‭ · 2021-07-25T18:07:08Z (almost 3 years ago)
> 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.