0
Can any one explain me inheritance in java programming
2 Answers
+ 2
class Animal {
int health = 100;
}
class Mammal extends Animal
{
}
class Cat extends Mammal
{
}
class Dog extends Mammal
{
}
public class Test
{
public static void main(String[] args)
{
Cat c = new Cat(); System.out.println(c.health);
Dog d = new Dog(); System.out.println(d.health);
} }
When running the Test class, it will print "100" and "100" to the console, because both, Cat and Dog inherited the "health" from Animal class.
0
Inheritance refers to an "is a " relationship in between two classes.
The most important use of inheritance is to avoid using repetitive codes or it enhances the code reusability of the java program.
for example a man is also a mammal with some attributes which are add-on to that of the mammal.