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);
    }
}
9
  • 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: blog.stackoverflow.com/2010/09/good-subjective-bad-subjective Commented Oct 28, 2013 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. Commented Oct 28, 2013 at 18:05
  • Read this javaworld.com/jw-11-1998/jw-11-techniques.html Commented Oct 28, 2013 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 ... Commented Oct 28, 2013 at 18:06
  • 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. Commented Oct 28, 2013 at 18:21

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.