0

Can any one explain me inheritance in java programming

23rd Jan 2018, 8:06 AM
Faiz Ahmad
Faiz Ahmad - avatar
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.
23rd Jan 2018, 4:46 PM
Arya DRJ
Arya DRJ - avatar
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.
24th Jan 2018, 8:58 AM
Aditya Jain
Aditya Jain - avatar