Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

How to "trim" a big class?

Thread view: 
Shawn - 15 Dec 2006 16:09 GMT
Hi,

I have a big class from somebody:

public class BigClass
{
    public void A()
    {
        ...//code
    }
    public void B()
    {
        ...//code
    }
    ...
    public void Z()
    {
        ...//code
    }

}

Now I want to make another class of mine, which only needs methods A(),
C(), M(). I don't want to extend BigClass because it is too big. How can
I do it?

One way I am thinking is:

public class MyClass
{
    BigClass big = new BigClass();
    public void A()
    {
        big.A();
    }
    public void C()
    {
        big.C();
    }
    public void M()
    {
        big.M();
    }
}

Thank you very much.
Oliver Wong - 15 Dec 2006 16:46 GMT
> Hi,
>
[quoted text clipped - 42 lines]
>
> Thank you very much.

Are you allowed to modify BigClass? Perhaps you could do something like:

public abstract class MySuperClass {
 public void A() {
   /*code*/
 }

 public void C() {
   /*code*/
 }

 public void M() {
   /*code*/
 }
}

public class BigClass extends MySuperClass {
 public void B() {
   /*code*/
 }

 public void D() {
   /*code*/
 }

 public void Z() {
   /*code*/
 }
}

public class MyClass  extends MySuperClass {
}

   - Oliver
Andreas Leitgeb - 15 Dec 2006 16:59 GMT
> Hi,
> I have a big class from somebody:
> Now I want to make another class of mine, which only needs methods A(),
> C(), M(). I don't want to extend BigClass because it is too big. How can
> I do it?

That doesn't make sense in my eyes.  Deriving from BigClass
doesn't mean that your derived class is necessarily big.
The code you inherit is not part of your class's code.

So finally, if you would derive from a smaller base class
then also derive from big base class.  If your class doesn't
really support the "is a"-relation to your particular base
class, then use delegation as you suggested. (that would
indicate a "has a" or "knows a"-relation)
Flo 'Irian' Schaetz - 15 Dec 2006 19:18 GMT
And thus, Shawn spoke...

> I have a big class from somebody:
>
> public class BigClass
...
> Now I want to make another class of mine, which only needs methods A(),
> C(), M(). I don't want to extend BigClass because it is too big. How can
[quoted text clipped - 9 lines]
>         big.A();
>     }

The big question is... WHY do you want to do this? Do you want to hide a
part of the interface? Do you want to activly forbid accessing the other
methods of your BigClass? In this case, your wayis a possibility. If you
just think, that your class gets "to big" (in memory terms): Forget it.
Simply derive your MyClass from your BigClass.

Flo


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.