[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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.
0
ajit_kamat
Asked:
ajit_kamat
  • 11
  • 9
  • 5
  • +4
1 Solution
 
setiawanCommented:
Java doesn't support for multiple inheritance (like C++).

use
class me extends MyFather implements MyMother{

}

  danny
0
 
ajaykshCommented:
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
0
 
sgomsCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ajit_kamatAuthor Commented:
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.
0
 
sgomsCommented:
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...
0
 
ajit_kamatAuthor Commented:
Hi All,

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

Thanks,

Ajit.
0
 
mbormannCommented:
0
 
jsridharCommented:
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.
0
 
mbormannCommented:
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
0
 
ajit_kamatAuthor Commented:
Hi jsridhar,

This is the excellent solution.

Thanks,

Ajit.
0
 
mbormannCommented:
Ajit,
u can asnwer this question for js by clicking on 'accept comment as question'
,i will definitely be bookmarking this one.
0
 
ajit_kamatAuthor Commented:
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.
0
 
jsridharCommented:
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.
0
 
Ravindra76Commented:
Superb
0
 
ajit_kamatAuthor Commented:
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.
0
 
jsridharCommented:
"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.

0
 
ajit_kamatAuthor Commented:
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.
0
 
jsridharCommented:
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.
 
0
 
sgomsCommented:
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
0
 
ajit_kamatAuthor Commented:
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.
0
 
jsridharCommented:
yes u guys are right. i overlooked somethings.
0
 
ajit_kamatAuthor Commented:
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.
0
 
mbormannCommented:
Yeah and i am going to ensure that I keep this bookmarked and tell it to as many guys as i can.
0
 
ajit_kamatAuthor Commented:
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.
0
 
mbormannCommented:
are ajit i am also marathi ,i stay at thane,what abt u?
:-)
0
 
sgomsCommented:
hey..u guys r from india? me too...its nice to see quite a few experts from india! ;-)
0
 
mbormannCommented:
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 ...)
0
 
ajit_kamatAuthor Commented:
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.
0
 
mbormannCommented:
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.

0
 
ajit_kamatAuthor Commented:
Hi Amit,

Sorry to hear that.

Bye.

Ajit
0
 
mbormannCommented:
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.
0
 
mbormannCommented:
hello all
i found a good site for this
http://www.artima.com/javaseminars/modules/DesWithCII/
just click around
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 11
  • 9
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now