-1

When should extends be used in stead of just using the class normally? Is it actually intended to override methods of the standard Java language that programers do not have access to?

Celeritas
  • 14,489
  • 36
  • 113
  • 194

3 Answers3

1

extends is used to Inheritence purposes in Java. You can inherit others classes in your class using this keyword.

Java documentation has a great tutorial for this: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

You extend the class when you want to implement the data from other class to your own class. Like the example in the comment section of the post.

Cat extends Mammal. Mammal extends Animal

You don't need to recreate the whole entire methods and data for the Cat, so you extend the Mammal class, Mammal which in turn has the properties and data for the Animals. This way, you can get all the properties and data of the animals in the Cat.

Then if you're having it like this

Animal Class

public Animal {
   private int type;
   private int height;
   private int width;
   // other private or public stuff
}

Animal inheritance by Mammal

public Mammal extends Animal {
   // here you'll have the type, height and width provided
   // to you as properties for the Mammal. You can use them
   // Mammal mammal = new Mammal();
   // int someType = mammal.type;
}

Mammal inheritence for Cat

public Cat extends Mammal {
   // since mammal has properties of Animal, 
   // cat would automatically get all the properties for Animal.
}

This way, you use the extend keyword. To get (inherit in Java) the properties of other classes.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • You miss the point. Yes you could extend but **when** should you? For example you could just say Dog class is extension of Animal, or you could just create an Animal object in the Dog class. – Celeritas Jul 04 '14 at 22:40
  • @Celeritas, Oh, now I get it. I hve editted the answer now :) – Afzaal Ahmad Zeeshan Jul 04 '14 at 22:42
  • "You don't need to recreate the whole entire methods and data for the Cat, so you extend the Mammal class" but you don't have to extend as you could just do `Mammal c = new Mammal()` as a member of the Cat class. So my question is how do you decide to take this approach over extends? – Celeritas Jul 04 '14 at 22:55
  • You can have multiple objects, some would be Animals as a whole, some would be Mammals (a bit classfied type of Animals) and among the Mammals you can have Cats, Lions etc. To distinguish and remove the ambiguity you use this approach. Otherwise you can even create an `Animal` object too inside the Cat class. But doing something like `Cat.Meow();` would be better rather than `Animal.Speak();`. Wouldn't it be? :) – Afzaal Ahmad Zeeshan Jul 04 '14 at 23:02
0

You use 'extends' when you want to have your class inherit and use some of the methods of the super class but add more specific operations to it. For example a Person class could have Student or Teacher classes which extend it.

public class Student extends Person

The Person class could have methods like getName() and getAge() while the Student class could inherit those methods while also have methods like getClasses().

0

If your class is a concrete one then you can directly instantiate and use it. Whereas, if you want to have a more specific class then you go for extend or inherit from more generic class.

Take an example of Honda Car class. If you have a specific model like Honda Civic zxsd. Now if you represent it as class then it's the most concrete one and you can directly use it by instantiating it.

Whereas the model Honda Civic when represented as class would a more generic one and if you want to represent a specific sub model civic zxsd as class you would want to inherit or extend from Honda Civic class.

Hope this makes sense.

Rahul
  • 76,197
  • 13
  • 71
  • 125