Basic Java query

hariharann
hariharann used Ask the Experts™
on
There are two classes Base1 and Base2 each having some protected members/methods. Now there should be a class DerivedFromBoth, which should be able to access the protected members/methods of both the classes. Since Java language supports only single inheritance, DerivedFromBoth can derive from only one of these two base classes and access only that classes protected data.

Note: <b>You can’t modify the Base classes.</b>

Base 1{
Protected b1;
Protected fb1(){}
}

Base 2{
Protected b2;
Protected fb2(){}
}

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
put the three classes in one package

Author

Commented:
Thanks.
Is there any other approach available? like using of an interfaces etc.?

Author

Commented:
Thanks.
Is there any other approach available? like using of an interfaces etc.?
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Commented:
don't think - at least the methods/members should not be protected. You can access protected members/methods only from the subclasses and the classes in the same package. So - your class could extend one of the classes and be member of the package of the second class.
Mick BarryJava Developer
Top Expert 2010

Commented:
If you just make the two interfaces then DerivedFromBoth can simply implement both interfaces.

public class DerivedFromBoth implements Base1, Base2
{
...


Author

Commented:
thanks Objects!
Can you please elaborate your answer further.
Like I should create 2 interfaces with the same method names as available in Base1 and Base2, then what should be done in the dervied class methods to be implemented?

Commented:
Yes you need to implement methods declared in classes Base1 and Base2in your DerivedFromBoth classes.
Commented:
1. You cannot "implement" a class:
public class DerivedFromBoth implements Base1, Base2

You have to derive classes that provide protected accessors to the need members and reimplement/wrap the methods.
Base1Derived extends Base1 {
 B1 getb1() {
   return b1;
  }
 void setb1(B1 b) {
   b1=b;
  }
 void FB1() {
  fb1();
  }

Then use a Facade:

public class DerivedFromBoth {
private Base2Derived b2;
private Base1Derived b1;
(protected|public) void fb1() {
  b1.FB1();
}
...
}

I hope that is clear enough...
1. if you are talking about real word use case:
class Sub1 extents Base1 {
  public void fb1() {
   super.fb1();
   ...
  }
  ...
}

do similar for Sub2

class AccessBoth {
  private Sub1 s1 = new Sub1();
  private Sub2 s2 = new Sub2();
  public void foo() {
    s1.fb1();
    s2.fb2();
  }
}

2. if you are talking about theoretics, NO!

3. otherwise, show you use case for help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial