I am new to android but Still I did not understand what is extends in java.
-
2Please go through documentations.Prashant– Prashant2020-03-17 03:00:09 +00:00Commented Mar 17, 2020 at 3:00
-
And don't completely change your question. Ask a new one, deleting the old one if it has no answers and no longer has relevance.user207421– user2074212020-03-17 04:07:42 +00:00Commented Mar 17, 2020 at 4:07
1 Answer
Forget about programming for now (I will come later)
- Think about an animal, Let's say Tiger. It jumps, it runs, it eats, it sleeps...
- consider another animal cat. it also jumps, eats, sleeps,....
- they have some common things between them. these behaviors are common for animals. They are extending the common behavior from their ancestors.
Now come to the Programming world
Let's create those animals in Java. Every animals have some common things among them. right?
class Tiger {
float weight;
float height;
public void jump() {
System.out.println("hey, See the jump");
}
public void eat() {
System.out.println("Tiger is eating, don't disturb");
}
}
class Cat {
float weight;
float height;
public void jump() {
System.out.println("hey, See the jump");
}
public void eat() {
System.out.println("I can eat veg");
}
}
but in programming world, We should not repeat. (Repeating a code is a not good practice). you can see jump has common code between cat and tiger. eating is differentiating.
So we can tell put those common things inside a Super class Animal. and extending them to cat and tiger
class Animal {
float weight;
float height;
public void jump() {
System.out.println("hey, See the jump");
}
public void eat() {
}
}
class Tiger extends Animal{
//it has Animal's jump method
@Override
public void eat() {
System.out.println("Tiger is eating, don't disturb");
}
}
class Cat extends Animal{
//it has Animal's jump method
@Override
public void eat() {
System.out.println("I can eat veg");
}
}
In above code, we reused jump method and changed eat method for cat and tiger.
The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance. The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be extended from the another class.
It is a concept of OOP. It is called Inheritance. Learn some concepts of inheritance here