-4

In my program, I am already extending the class Applet , but I also would like to get to methods I have defined in another class. I am aware that I cannot extend 2 classes at the same time, is there a way I could get around this? I need to be able to do this in order to continue, thanks.

Here's my code:

import java.applet.Applet;
import java.awt.*;
import java.awt.Graphics;

public class Grape extends Applet /* I want to extend another class here */{

    public void paint (Graphics page) 
    {
        final int MID = 150;
        final int TOP = 50;

        setBackground (Color.WHITE);

        page.drawLine (60, 60, 750, 60); //Line
        page.drawString("X discplacement = " + xDisplacement , 30, 30);



    }

}
HLatfullin
  • 13
  • 2
  • 8
  • you can create an object of that class to access those methods – Sagar Pudi Nov 03 '14 at 17:23
  • 1
    possible duplicate of [Java Multiple Inheritance](http://stackoverflow.com/questions/21824402/java-multiple-inheritance) – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Nov 03 '14 at 17:24
  • No you don't "need it to continue". There is no multiple inheritance in java (and there isn't in a bunch of other languages as well). Instead of wishing for the non-existing, use delegation/composition and continue making progress. In the time you wasted asking for the impossible you could already have implemented the solution. – Durandal Nov 03 '14 at 17:30
  • You may want to use an `interface`, they work similarly to super-classes but don't provide a default implementation of methods or values of variables. – SamTebbs33 Nov 03 '14 at 17:50

3 Answers3

1

Because of diamond problem. Java avoids the diamond problem by enforcing single-inheritance for classes.

JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions.

-- James Gosling, on diamond problem

Please visit the following links to know more about the problem.

Similar question answered on SO

External links:-

[EDIT]

How to implement multiple inheritance in Java? interface

Java allows multiple inheritance for interfaces.

Community
  • 1
  • 1
Mitesh Pathak
  • 1,306
  • 10
  • 14
0

You could make your other class extend Applet and then let "Grape" extend your other class. But of course, this is not exactly what you need sometimes.

Also you could store an instance of your other class within Grape. But this does not solve the problem completely either.

Michael
  • 815
  • 1
  • 6
  • 23
0

First: Title of your question is miss-leading. Please choose a better title for your questions.

Second: You can inject functionality of other classes to your 'Grape' class, by making a reference to them.

Sometimes, redesigning your classes, relation between them, and redesigning flow of your application, solve your problem in a more convenient way.

MK Aftab
  • 39
  • 7