Reputation: 9692
I have defined a superclass as follows;
public class superClass{
private superClass superObj;
public superClass() {
}
public superClass(superClass superObj) {
this.superObj = superObj;
}
public start()...
public receive()..
And my subclass is like;
public class subclass extends superclass {
@Override
public receive(){
........
}
public static void main(String args[]){
Superclass superTst = new Superclass(new subclass());
superTst.start();
...
}
Here, My subclass is not actually "is-a" relationship with superclass. But I want to extend like this, because, in the superclass I do some processing output (superclass spawns new thread and do processing, so I can get the output at superclass), which has to be accessed by subclass.
Is this theoretically right ?
Upvotes: 0
Views: 88
Reputation: 4256
As you mentioned that
subclass is not actually "is-a" relationship with superclass
So I suppose you should use composition instead of inheritance as shown below:
public class superClass{
private subclass subclassObj;
public superClass() {
}
public superClass(subclass subclassObj) {
this.subclassObj = subclassObj;
}
public start(){
//some code
}
public receive(){
subclassObj.receive(); // call subclass' receive() method
}
And your subclass
can be like:
public class subclass {
public receive(){
//some code
}
public static void main(String args[]){
Superclass superTst = new Superclass(new subclass());
superTst.start();
// ...
}
}
Upvotes: 0
Reputation: 558
Lets suppose that you want to create 10 threads in the SuperClass:run() method, each thread calling a SuperClass::execute() method that should run on the final instance (subclass) and not in SuperClass:
abstract class SuperClass {
protected abstract function getClass();
protected abstract function exec($thread_id, $data);
public function run(){
for($i=0;$i<10;$i++){
// create a new instance of the very top sub class
$child_instance = new $this->getClass();
$this->createChildThread($child_instance, $this->someData);
// suppose that each thread will call: $child_inst->exec();
}
}
}
class SubClass extends SuperClass {
public function getClass(){ return __CLASS__; }
public function exec($thread_id, $data){
// hi...i'm executing inside a thread_id
// this instance, is a new instance of class Subclass and
// not a SuperClass instance :)
}
}
Upvotes: 0
Reputation: 100
You should be able to call any method of your super class from your subclass. Also, any time a subclass is instantiated, its super class default constructor is called. You can call another constructor with the super
keyword.
To answer the question directly, it is not good design to construct a subclass using a super class object.
Upvotes: 0
Reputation: 2624
I would not do so. If the subclass has a dependency on the superclass, do dependency injection there. It will be more clear what the dependencies are and maintain the single responsibility principle. Subclassing something that cannot stand in for its superclass will ultimately just be confusing and means you are tightly coupling implementations.
Upvotes: 1