brownie124
Programmer
Hi,
I was curious as to what the common practice is regarding subclasses is? Is it common practice to use subclasses as opposed to creating a brand new top level class? Or does it depend on things like:
- Will the class be instantiated by any other classes really? If not, then it probably should be a subclass, right? If this is the case, then why not just create a function instead of a subclass?
- Scope of variables -- will your classes need to access variables in the class that instantiated the class or subclass? (Leads to another question below)
- Preference. I know I don't like the long names that are created by the compiler for subclasses.
The second item leads me to ask this question:
If a top level class does instantiate another top level class and the class that is being instantiated needs to access variables from the instantiating class, is it acceptable for the instantiating class to pass itself as a parameter. For example:
This is just an example, I know there are other ways to do things like this. I just want to know if things like this are acceptable.
Thanks,
- Michael
I was curious as to what the common practice is regarding subclasses is? Is it common practice to use subclasses as opposed to creating a brand new top level class? Or does it depend on things like:
- Will the class be instantiated by any other classes really? If not, then it probably should be a subclass, right? If this is the case, then why not just create a function instead of a subclass?
- Scope of variables -- will your classes need to access variables in the class that instantiated the class or subclass? (Leads to another question below)
- Preference. I know I don't like the long names that are created by the compiler for subclasses.
The second item leads me to ask this question:
If a top level class does instantiate another top level class and the class that is being instantiated needs to access variables from the instantiating class, is it acceptable for the instantiating class to pass itself as a parameter. For example:
Code:
public class Foo1
{
String foo1String = "Hello";
public Foo1()
{
Foo2 foo2 = new Foo2(this);
}
} // end Foo1 class
class Foo2
{
String foo2String = "World";
Foo1 foo1;
public Foo2(Foo1 foo1)
{
this.foo1 = foo1;
System.out.println(foo1.foo1String + " " + foo2String);
}
} // end Foo2 class
This is just an example, I know there are other ways to do things like this. I just want to know if things like this are acceptable.
Thanks,
- Michael