I am new to Java and am learning class keywords. I have been given limited examples, but I am trying to understand the reasons why you would use an instance object over extending an object. In the tutorials I have seen there is a book class and a magazine is an instance of book. But if the class is abstract then you cannot have an instance only an extension. Why would I use extension over an instance and vice versa? Can anyone provide an example of when it would be better to use one over the other?
Asked
Active
Viewed 31 times
0
-
If I understand correctly you're asking [when to use composition over inheritance?](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – pafau k. May 29 '20 at 23:22
-
Are you referring to composition vs inheritance? If no, can you provide some code to give more context? – Andrew S May 29 '20 at 23:23
-
1You seem to think that extending a class and making an instance are similar things, but they're not. When you extend a class, you can add methods and fields to it or override existing methods. When you make an instance of an object, you can actually use those methods (let's ignore static methods for now). – user May 29 '20 at 23:27