2

I have an assignment with several abstract classes and including inheritance, but I've got a problem in a specific place of the assignment.

This is what I have (close up to the specific place where the problem going to happen):

public abstract class A {
    //some fields
    //constructor
    //some functions
}
public abstract class B extends A {
    //some fields
    //constructor
    //override functions
    //some new functions
}
public abstract class C extends A {
    //some fields
    //constructor
    //override functions
    //some functions
}
/* class D that extends both B and C */

Now I need to create class D that extends class B and C.
I know that multiple inheritance can't be used in java so:

  1. I thought about delegate DP but I can't create 2 instances of class B and C (nor extend one and create instance of the other as they are both abstract).
  2. I also thought maybe extend the class with more complex functions/more fields and create interface of the other class but that will make me add all the fields from that class in my new class(I don't think it's the "right way").
  3. also I'm not sure if changing either of the already existing classes is what I need to do as the assignment.

in other words I'm lost...

1 Answers1

2

You cannot create class D that extends both B and C.

Instead, consider using interfaces, such as treating A, B, C as interfaces.

public interface A {
    //some functions
}
public interface B extends A {
    //some functions
}
public interface C extends A {
    //some functions
}
public abstract class D implements C,B {
    ...
}
  • But then again, class D will have to "cope/paste" all the fields and functions that already implemented in the other classes, I thought of it as already stated, but is copy/pasting code really the right answer? – Seriy Alexandrovski May 13 '18 at 01:10
  • Default methods can work, but that is something more advanced. Suppose A is a vehicle, B is a car, C is an aircraft, and D is a flying car (both a car and aircraft). Or how about letting D extend A, implements B and C which extends A? See this page about the diamond problem :http://www.lambdafaq.org/what-about-the-diamond-problem/ – Ṃųỻịgǻňạcểơửṩ May 13 '18 at 01:14
  • As close you assumed my problem, the link resolves different methods, what if all classes have toString() like in my case? and still I have to copy/paste all the fields from classes B and C to class D with all the implementations, you're still basically saying "just copy/paste everything". – Seriy Alexandrovski May 13 '18 at 01:23
  • It is not recommended use toString() for abstract classes. Sorry about my previous comment, but try letting A be the abstract class, B,C be interfaces. – Ṃųỻịgǻňạcểơửṩ May 13 '18 at 01:25
  • But just like in your example, vehicle have its fields "written" in the toString(), a car have additional info into the toString() as the C have additional info to aircraft, and now class flying car need to have ALL the info from both car and aircraft! so now I either must copy/paste everything from the classes car and aircraft (fields/methods) or choose one to extend and the other still to copy/paste, which resulting the same question, is copy/paste really the "right" answer? – Seriy Alexandrovski May 13 '18 at 01:29
  • As far as I saw, you can either try default methods, or treat A as the abstract class, B,C as the interfaces, and use some copy/paste. Sorry about that, but multiple inheritance can make stuff more complex. In some cases, use `B.super` or `C.super`. – Ṃųỻịgǻňạcểơửṩ May 13 '18 at 01:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170928/discussion-between-mulliganaceous-and-seriy-alexandrovski). – Ṃųỻịgǻňạcểơửṩ May 13 '18 at 01:40