Trouble with understanding extends and instantiate. Is extends the same as instantiating a class?

173 views Asked by At

My question is. Are these the same?

public class Pet {
}

public class Fish extends Pet {
}

If I extend the class Pet to my Fish class, is that the same as if I instantiate the Pet class in my Fish class? The extends is above and the instantiate is below. Are they the same?

public class Pet {
}

public class Fish {

Pet myPet = new Pet ();
}

4

There are 4 answers

4
marknorkin On BEST ANSWER

First example describes inheritance, the second one - composition. Those are two OOP concepts. They allow programmer to reuse common logic. Your should prefer to use composiiton over inheritance.

Copy from other SO answer:

They are absolutely different. Inheritance is an "is-a" relationship. Composition is a "has-a".

You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would've been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now considered a blunder. A stack "is-NOT-a" vector; you should not be allowed to insert and remove elements arbitrarily. It should've been composition instead.

Unfortunately it's too late to rectify this design mistake, since changing the inheritance hierarchy now would break compatibility with existing code. Had Stack used composition instead of inheritance, it can always be modified to use another data structure without violating the API.

I highly recommend Josh Bloch's book Effective Java 2nd Edition

Item 16: Favor composition over inheritance

Item 17: Design and document for inheritance or else prohibit it

Good object-oriented design is not about liberally extending existing classes. Your first instinct should be to compose instead.

0
alzee On

They are not the same.

In the first example, with inheritance, an instance of a Fish can access all of it's own properties and methods, including those inherited from Pet, via this or self, depending on language.

In the second example, myPet is just a variable that happens to be an instance of the Pet class, but Pet and Fish have no relationship to each other.

3
SteelToe On

No, They are completely different.

In this one public class Fish extends Pet { } you are using inheritance to extend the Pet class to the Fish class, meaning that Fish is a subclass of Pet it will inherit the Pet class.

However in this one

 public class Fish {

 Pet myPet = new Pet (); }

You are creating a whole new object called Fish which does not extend from anything, just that it has a class level object that is a Pet, so you can use the Pet objects methods variables etc through the myPet object however it is not inherited by Fish so Fish would be its own object and not a subclass of Pet.

These are the differences.

As for when you should use which, here is the general rule: if you are enhancing a class then you should use Inheritance, however if you are just going to be using a class for its particular function then you should instantiate it as a variable in the class.

0
dhamu On

They are completely different. extends is 'is-a' relationship , while later (composition) is 'has-a'. Take a look here for more details.