Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance

 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

I have read many docs regarding multiple inheritance in java.

I know that in java one class can extend only one class but can implement many interfaces, but still my doubt remains unclear.

If i have 3 classes

public class MyClassA{
}

public class MyClassB{
}

public class MyClassC{
}

how can inherit the properties of the 3 classes. I read that multiple interfaces can be implemented. But interface cannot extend a class. So how will interface use the properties of class?

Lets say that my main intented class is

public class AllClassProperties which will have certain selected properties same as the three( i am not saying common properties, it is only selected properties and their methods)

how can i bridge between AllClassProperties and the three myclasses?

CAN WE USE INTERFACE INSIDE A CLASS?

please help me with a complete example or a link if you can...

thanks for any help in advance.
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create interfaces for each of those classes (InterfaceA for MyClassA etc.), and have



Eclipse has support for delegation (Source -> Generate Delegate Methods).

If you search for composition inheritance, you'll find many articles, e.g.
http://www.artima.com/lejava/articles/designprinciples4.html
 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, i'l try it out.
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tarun saha wrote:hmm, i'l try it out.


In general, try not to create classes that are overcomplicated. Implementing several interfaces will bring lots of public functions; the delegate methods will mean lots of code (even though generated). Check whether you can simplify the design, whether there's no way to avoid this class taking on so much functionality/responsibility.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic