在Java编程语言中,”extends”是用来表示继承关系的关键字。

huangapple go评论77阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年3月16日 09:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/60699423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定