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?
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?
Post
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.
1 comment thread