Link to home
Start Free TrialLog in
Avatar of ajit_kamat
ajit_kamat

asked on

Problem with inheritance.

Hi,

I have two classes "MyFather" and "MyMother" which have their own set of attributes and methods implemented. I want to construct a new class "Me" which inherits from both "MyFather" and "MyMother". How do I implement this in Java ?

Thanks,

Ajit.
Avatar of setiawan
setiawan

Java doesn't support for multiple inheritance (like C++).

use
class me extends MyFather implements MyMother{

}

  danny
hi,

As one of the good things in Java - multiple inheritance is NOT possible.

however u can mimic multiple inheritance using Interfaces, for this u have to create one interface for MyFather and one for MyMother, put the respective methods in them [strictly as abstract methods], and then construct ur class 'Me' by implementing interfaces MyFather and MyMother. This way you have to provide the implementation of the methods in 'Me'.

For example:
-----------------------------------------------------------------


public interface MyFather
{
       abstract void mF();
}

public interface MyMother
{
       abstract void mM();
}

public interface Me implements MyFather, MyMother
{
     void mF()
     {
      ...
     }

     void mM()
     {
      ...
     }

}

-----------------------------------------------------------------
This is not a short coming in java but a feature.

Thanx
yes..Java does not support multiple inheritance. instead u have interfaces.
u can implement any number of interfaces.
instead of class if you create MyFather and MyMother as interfaces, then you can have,

class child implements MyFather,MyMother{
}

An Interface is basically a abstract calss with,
1."only" public abstract methods
2."only" final static variables

check out the java tutorial on interfaces,
http://java.sun.com/docs/books/tutorial/java/javaOO/implementing.html

'Thinking in Java'
http://www.javathings.com/tij/tij0023.html#Heading28
Avatar of ajit_kamat

ASKER

Hi ajayksh,

I want to inherit the member variables and methods implemented by both "MyFather" and "MyMother" in class "Me". I am not the designer of the classes "MyFather" and "MyMother", I only want to reuse its functionality.
Making them as interfaces is like implementing all the classes from scratch. This is not acceptable to me.

Thanks,

Ajit.
Ajit iam afraid multiple inheritance is not a feature available in java!
i guess u have settle for creating a object and accessing the variables & methods...
Hi All,

Ok. I think I have to live with it.
No multiple inheritance only composition.

Thanks,

Ajit.
Hi Ajit,

Instead of considering only "Multiple Inheritance" hierarchy, you can think about "Containment" hierarchy.

This kind of hierarchy is found in object models like DOM, for eg. in JavaScript/VBScript you say location.href = "xxxx", here "href" is a object member of location.

You can try to write a wrapper class like this:

class MyParents {

  private void MyFather father;
  private void MyMother mother;

  public MyParents(MyFather pop, MyMother mom) {
    father = pop;
    mother = mom;
    ...
  }

  //other methods which explicitly extract info from either father or mother.

}

Since you know the members of both MyFather and MyMother, you can decide which one to use when u design your class MyParents.

js.
js,
that is an excellent suggestion ,I have seen it used but man i forgot abt it,way to go man way to go.

I think this is the most elegant solution to the problem of multiple inheritance in Java.

Regards
Hi jsridhar,

This is the excellent solution.

Thanks,

Ajit.
Ajit,
u can asnwer this question for js by clicking on 'accept comment as question'
,i will definitely be bookmarking this one.
Hi mbormann,

True. I accept js's explanation as an answer. That solves my problem.

But one small doubt :

What if class "Me" is in another package than the classes "MyFather" and "MyMother" and I want to inherit the protected members of both of these classes.

Thanks,

Ajit.
When you are having "MyFather" and "MyMother" in another package, you have to write sub-classes to both "MyFather" and "MyMother" in your package.

public class MyFatherWrap extends MyFather {

  public MyFatherWrap() {
   super();
  }

  public int methodName() {
     return super.methodSomethingElse();
  }
}

Here the names of the methods for the protected methods should be changed, bcos the scope of the method is changed.

And then can use the "Containment" hierarchy.
Superb
Hi js,

I did not get the meaning of the lines :

"Here the names of the methods for the protected methods should be changed, bcos the scope of the method is changed. "

Can you please explain it.

Thanks,

Ajit.
"When a class is sub-classes, the scope of the members cannot be expanded"

It means: a parents class protected member cannot be declared as child's public member

eg.:

class Parent {

  protected void method() {
  }

}

class Child extends Parent {

  public void method() {
  }
}

The above cannot be done. So, u can do something like this:

class Child extends Parent {

  public void callMethod() {
    method();
  }
}

Here the protected method is exposed, in some sense.

js.

Hi js,

Cann't we keep the child method's access specifier as protected in this case? If we can, then there is no need to change the name of the child method.
This will keep the interface consistent.

Bye,

Ajit.
If you keep the access specifier as protected itself, then the MyParents class will not be able to access those members of the MyFatherWrap or MyMotherWrap.

The objective of this scope change is to provide access to all those methods, that you think, in MyFatherWrap/MyMotherWrap from MyParents.

Hope this clarifies all your doubts.

js.
 
js,

ofcourse u can have,
class Parent {
     protected void method() { }
}
class Child extends Parent {
      public void method() { }
}

what u cannot have is,

class Parent {
   public void method() {
   }
}

class Child extends Parent {
   protected void method() {
   }
}

'cos this wud produce an error,
"The method void method() declared in class Child cannot override the method of the same signature declared in class Parent.  The access modifier is made more restrictive."

access specifiers cannot be made more restrictive.u can make a protected method more public but not more friendly!
the order of restriction is,

private -> "friendly" -> protected -> public
Hi js,

I agree with sgoms.But what I have tried out is something like this :

=============================================================

package com.MyParents;

public class MyFather
{
      protected void protectedMethodFather()
      {
            System.out.println( "MyFather's protected method called." ) ;
      }
}

=============================================================

package com.MyParents;

public class MyMother
{
      protected void protectedMethodMother()
      {
            System.out.println( "MyMother's protected method called." ) ;
      }
}

=============================================================

package com.Me;

import com.MyParents.* ;

class MyFatherWrap extends MyFather
{
      protected void protectedMethodFather()
      {
            System.out.println( "MyFatherWrap's protected method called." ) ;
            super.protectedMethodFather() ;
      }
}

class MyMotherWrap extends MyMother
{
      protected void protectedMethodMother()
      {
            System.out.println( "MyMotherWrap's protected method called." ) ;
            super.protectedMethodMother() ;
      }
}

class Me
{
      private MyFatherWrap mfw = new MyFatherWrap();
      private MyMotherWrap mmw = new MyMotherWrap();
      
      protected void protectedMethodFather()
      {
            System.out.println( "Me's protected method (father) called." ) ;
            mfw.protectedMethodFather() ;
      }
      
      protected void protectedMethodMother()
      {
            System.out.println( "Me's protected method (mother) called." ) ;
            mmw.protectedMethodMother() ;
      }
}

=============================================================

package com.Me ;

public class Inheritance
{
      private Me me = new Me() ;
      public static void main (String[] args)
      {
            Inheritance i = new Inheritance() ;
            i.me.protectedMethodMother() ;
            i.me.protectedMethodFather() ;
      }
}

=============================================================

I think this maintains the access control of the parent class methods as well as keeps the interface consistent.

Any Suggestions/Corrections on it ?

Thanks,

Ajit.
ASKER CERTIFIED SOLUTION
Avatar of jsridhar
jsridhar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi js,

Your explanation was excellent.
sgoms also cleared the doubts at the end.

Merry computing to all of you who tried to help me out.

Thanks all.

Ajit.
Yeah and i am going to ensure that I keep this bookmarked and tell it to as many guys as i can.
Hi mbormann,

True. The discussion was insightful and thanks also for your advise on the issue. The discussion shows that it is possible to mimic multiple inheritance by a simple trick. Though multiple inheritance has disadvantages it might not be possible for us to avoid it in ceratin situations. It is where this discussion is helpful.

Thanks.

Ajit.
are ajit i am also marathi ,i stay at thane,what abt u?
:-)
hey..u guys r from india? me too...its nice to see quite a few experts from india! ;-)
hey u a Goan? I am going to goa on 25th or 29th ,
man i am glad u 2 an indian and jshridhar too!

sgoms ,wher u stay?am waitng for ur reply online,mail me at amit@eparle.com (company acct ,so be formal ...)
Hi mbormann and sgoms,

mbormann - Does't this sound like a German name ? I thought that you might be from Germany. Surprise to here that you are an Indian. Why did you choose this name ?

I am Marathi. I am from Nashik. But presently I am working in Pune.

sgoms and jshridhar - Please send me your e-mail ids.

My e-mail account is ajit@jopasana.com
It's my company account.


Thanks,

Ajit.
I had a friend who was called Martyn Bormann ,so as a lark I took on his name, he was a good friend of mine , he was teaching at Max Mueller Bhavan.He taught me English literature ,introduced me to quite a nice start in Life,unfortunately he was suffering fom a terrible disease & as he jokingly used to say ,he copped out.

Hi Amit,

Sorry to hear that.

Bye.

Ajit
no problem ,it was nice while he was there.and ajit why u so formal? loosen up man ,and enjoy life short as it is.
hello all
i found a good site for this
http://www.artima.com/javaseminars/modules/DesWithCII/
just click around