英文:
What is extends in Java Programming Language
问题
我是新来的安卓开发者,但我还不理解在Java中的"extends"是什么意思。
英文:
I am new to android but Still I did not understand what is extends in java.
答案1
得分: 1
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 animal has 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 the programming world, we should not repeat. (Repeating code is not a good practice). You can see that the jump
method has common code between cat and tiger. Eating is differentiating.
So we can put those common things inside a Super class Animal, and extend 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 the above code, we reused the jump
method and changed the 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 the rest of the common properties and functionalities can be extended from another class.
It is a concept of Object-Oriented Programming (OOP). It is called Inheritance. Learn some concepts of inheritance here.
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论