How to "trim" a big class?

S

Shawn

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.
 
O

Oliver Wong

Shawn said:
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.

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
 
A

Andreas Leitgeb

Shawn said:
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)
 
F

Flo 'Irian' Schaetz

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
I do it?

One way I am thinking is:

public class MyClass
{
BigClass big = new BigClass();
public void A()
{
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top