Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Acceptable practice?

Status
Not open for further replies.

brownie124

Programmer
Sep 19, 2002
61
US
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:
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
 
I said "function" but obviously meant "method".
 
>The second item leads me to ask this
For the second question regarding the variable foolString, you always want to declare it as private. Then, use public accessor methods so others can get to it. Hence, from Foo2,
it it preferable to do this:-

System.out.println(foo1.getFooString + " " + ...

You never want other objects to be able to see other object private methods directly. What if later on, you want to add some business rules before passing FooString, then other people that call your objects have to change their way of calling FooString too. But with getFooString, you can add rules in there and not have to worry that it would break some other people's codes.


~za~
You can't bring back a dead thread!
 
Michael,

When you say "subclass", the rest of your post sounds like you mean "inner class" ?
 
the concept of inner class is kind of "indistinct" in terms of what it is used for. Principally, if you need to create a class that is so unique inside another class that the method is 'useless' to other objects outside the outer class, then it is proper to use it.

But, The paradigm is revolutionary and worth to be thought about. I actually used it once it my codes a couple of years back - when all other solutions didn't make sense to that dilemma in design.

~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top