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);
}
}