6

I'm having trouble with the concepts of object oriented programming. Is it better to extend a class or create a new object within a class? Under which circumstances would you extend a subclass versus creating a new instance of that subclass within the calling class? The example is in Java but I imagine the concepts will work in other OOP languages.

I appreciate your insights

class Mini {
// I want to use the members of this class in another class
    int myInt;

    public Mini(int myInt){
        this.myInt = myInt;
    }

    public int myMethod(){
        return this.myInt;
    }
}

// should I create a new instance of Mini here?
class Maxi {
    Mini myMini = new Mini(5);

    public Maxi(){
        int miniInt = myMini.myMethod();
        System.out.print(miniInt );
    }
}

// or should I have Maxi extend Mini?
class Maxi extends Mini {
    public Maxi(int myInt){
        System.out.print(this.myInt);
    }
}
DataCat Robin
  • 624
  • 1
  • 6
  • 12
  • This question seems prone to encourage more opinion-based answers rather than strictly objective answers. Frequently, such questions are not a good fit for Stack Overflow. See: http://blog.stackoverflow.com/2010/09/good-subjective-bad-subjective/ – BlackVegetable Oct 28 '13 at 18:04
  • 1
    As a rule of thumb: if you can achieve your goal easily without creating a subclass, you shouldn’t create a subclass. Well, unless an obvious “x *is a* y” relationship exists. – Holger Oct 28 '13 at 18:05
  • Read this http://www.javaworld.com/jw-11-1998/jw-11-techniques.html – Prateek Oct 28 '13 at 18:05
  • They are two very different things. Extending the class you create an 'is a' relationship i.e Maxi is a type of Mini. If you instantiate a Mnin within a Maxi then it is a 'contains' relationship ... – avrono Oct 28 '13 at 18:06
  • Doesn't matter. Just don't write dumb code... And learn another languare, or two. Java has its limitations, so it's not cool to always "think in Java". – Display Name Oct 28 '13 at 18:08
  • Its depending on what you wanna archieve, for example: if you have a class called `Car` and a subclasses `Mercedes` and `BMW` you should definitly extend, because you need the methods from the superclass in both subclasses. But if those classes dont have a relationsship like my example you can just do it like you did it: `class Maxi { Mini myMini = new Mini(5); ...` – Leviathan Oct 28 '13 at 18:09
  • 1
    @luke: **No**. In most cases it’s enough having “Mercedes” and “BMW” being instances of car brands. They do *not* add something new to the concept of a car. It’s the same as as making “RedCar” and “BlueCar” subclasses of “Car”. Don’t do it. – Holger Oct 28 '13 at 18:21
  • @Holger well you are right, may my example wasn't the best ;-) – Leviathan Oct 28 '13 at 18:24
  • Thanks guys. Sorry for the duplicate question. Honestly, I didn't have the proper vocabulary to search for answers to my question effectively and my "college level" java classes have been going over loops for like four semesters. Since it is a repeat, should I delete this question? – DataCat Robin Oct 29 '13 at 14:37

5 Answers5

4

You extend a class when it has an is-a relationship. For example, a Cat is an Animal. A Cat is not a CatFood but it might use CatFood.

In your case, I'm not sure what MiniMaxi are, but it doesn't sound like Maxi is a Mini, rather it uses one.

In general, try to favor composition (using objects) over inheritance, especially in single inheritance languages like java.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
1

The best answer to that question is another question: does class B use class A or does class B build on class A?

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

Well one question you should ask yourself is what other uses will this class have? Will you still need the functionality of this class in the subclass? Will you have more classes that will also inherit mini?

The goal is to save duplication. You need to know more about your use case to be sure.

I would really only inherit if you have more than one thing extending the top class and using its functionality and variables. If you are just using for a method signature interface is usually a better bet.

ford prefect
  • 7,096
  • 11
  • 56
  • 83
0
  • In first case is an aggregation relation, named has-a. Class Maxi has a attribute, whose type is class Mini.
  • In second is an inheritance relation, named Is-a. Class Mini is subclass of class Maxi and Maxi is supclass of class Mini. When a class Maxi is extended, the new class inherits all the fields (protected or public data and methods) of the class Mini.
0

The distinction between "is-a" and "has-a" is very important and critical. If you are not sure about "is-a" relation, never use inheritance. So if you know that conceptually B is not A then don't use inheritance for B. Also if you are being tempted to inherit from a class just to reuse some functionality, never do that - use "has-a" composition/aggregation techniques.

Shailendra
  • 8,874
  • 2
  • 28
  • 37