Inheritance in Java
Inheritance is an important concept in Object Oriented Programming, also called OOPS. It allows one class to inherit the attributes and methods of another class.
Some important terms:
Cats are animals. Dogs are also animals.
They both eat.
They both have 4 legs.
They both make sound. While a cat says "meow", a dog says "woof".
So why not take the common things between them, and put it in an Animal class? We can then have both Dog class and Cat class "inherit" stuff from Animal?
Let me show with some code:
Now, as you can see, both Dog and Cat "inherited" the attribute: numberOfLegs and the eat() method.
They are able to "override" the makeSound() method with their own implementation.
This is what is called Inheritance. Seriously, it's that simple!
Here is a simple diagram illustrating this concept.