-
Multiple inheritance
I like to know how to solve multiple interitance.
I created one class file(A). I extends that class in another class file(B).
In file B, I am going to use getParameter method.
If I want to use getParameter method, I have to extend Applet class. But
I got error messages which is warning as Java does not support multiple inheritance.
How can I solve this problem ?
Please advise me.
I will appreciate any advice. I am really beginner.
-
Re: Multiple inheritance
In java u cannot derive a class from two classes. U need to write a interface
and implement the interface.
Hope this helps.
"Travis" <trabis@hotmail.com> wrote:
>
> I like to know how to solve multiple interitance.
>
> I created one class file(A). I extends that class in another class file(B).
>In file B, I am going to use getParameter method.
>If I want to use getParameter method, I have to extend Applet class. But
>I got error messages which is warning as Java does not support multiple
inheritance.
>
> How can I solve this problem ?
>Please advise me.
> I will appreciate any advice. I am really beginner.
>
-
Re: Multiple inheritance
Hello Travis:
You've run into one of the things that makes Java easier than C but it can
also be a straightjacket. Here's a possible solution:
Make class A an interface then implement it in class B. Class B can then
extend Applet.
Using this approach, however, can be a problem as well. Since interfaces
can only contain method signatures and final variables(constants), you'll
need to move all of your instance variables to class B. You'll also need
to implement ALL of the methods in class A in class B. Implementing interfaces
really just means that classes that implement them will implement the same
methods and include the same final variables.
Not much help - sorry,
Tom Duffy
"Travis" <trabis@hotmail.com> wrote:
>
> I like to know how to solve multiple interitance.
>
> I created one class file(A). I extends that class in another class file(B).
>In file B, I am going to use getParameter method.
>If I want to use getParameter method, I have to extend Applet class. But
>I got error messages which is warning as Java does not support multiple
inheritance.
>
> How can I solve this problem ?
>Please advise me.
> I will appreciate any advice. I am really beginner.
>
-
Re: Multiple inheritance
One way to solve this is by delegation. Instead of extend-ing the second
class, include a private variable of that class. Then define that class's
methods as methods of your class and have them execute the corresponding
methods of the private variable. As with interfaces, you have to specify
all of that class's methods; they won't automatically exist. And if that
class has public variables (a practice somewhat frowned on in the Java
programming world), you'll need to work out how to make them appear to be
public variables of your new class.
So instead of
public class MyClass extends Applet, Wombler /* doesn't work */
you can use
public class MyClass extends Applet
private Wombler secretWombler;
public MyClass() {
secretWombler = new Wombler();
}
public void rotate(int degrees) {
secretWombler.rotate(int);
}
and so on.
It's probably better to extend Applet, if you want your class to behave like
an applet, and delegate the other class as in the example above.
Travis <trabis@hotmail.com> wrote in message
news:38cedf2b$1@news.devx.com...
>
> I like to know how to solve multiple interitance.
>
> I created one class file(A). I extends that class in another class
file(B).
> In file B, I am going to use getParameter method.
> If I want to use getParameter method, I have to extend Applet class. But
> I got error messages which is warning as Java does not support multiple
inheritance.
>
> How can I solve this problem ?
> Please advise me.
> I will appreciate any advice. I am really beginner.
>
-
Re: Multiple inheritance
To add to Paul's response,
IMHO, unless you're just messing around, you don't want to make ANY variables
in a class public. You just need "setter" and "getter" methods - "setter"
methods that accept an argument(s) and then set a private member variable(s)
equal to the argument(s) passed in, and "getter" methods that simply return
a private member variable.
Hope this helps a little,
Herbie Wilson
"Paul Clapham" <pclapham@core-mark.com> wrote:
>One way to solve this is by delegation. Instead of extend-ing the second
>class, include a private variable of that class. Then define that class's
>methods as methods of your class and have them execute the corresponding
>methods of the private variable. As with interfaces, you have to specify
>all of that class's methods; they won't automatically exist. And if that
>class has public variables (a practice somewhat frowned on in the Java
>programming world), you'll need to work out how to make them appear to be
>public variables of your new class.
>
>So instead of
>
>public class MyClass extends Applet, Wombler /* doesn't work */
>
>you can use
>
>public class MyClass extends Applet
>private Wombler secretWombler;
>public MyClass() {
> secretWombler = new Wombler();
>}
>public void rotate(int degrees) {
> secretWombler.rotate(int);
>}
>
>and so on.
>
>It's probably better to extend Applet, if you want your class to behave
like
>an applet, and delegate the other class as in the example above.
>
>Travis <trabis@hotmail.com> wrote in message
>news:38cedf2b$1@news.devx.com...
>>
>> I like to know how to solve multiple interitance.
>>
>> I created one class file(A). I extends that class in another class
>file(B).
>> In file B, I am going to use getParameter method.
>> If I want to use getParameter method, I have to extend Applet class. But
>> I got error messages which is warning as Java does not support multiple
>inheritance.
>>
>> How can I solve this problem ?
>> Please advise me.
>> I will appreciate any advice. I am really beginner.
>>
>
>
-
Re: Multiple inheritance
Hi Travis ,
I have a soln in mind and it is already being used in UI .. programming involving
Adapter Classes
the soln does not invlove interfaces so relax .. and it ia practical too:
Suppose U have two classes X and Y which U want to inherit in U'r class
class X
{}
class Y{}
when U inherit .. actually U want the existing functionality of both the
classes
so U can write a class as
class outer extends X
{
method(a)
{}
class inner extends Y
{}
}
Now follow the reasoning
1)An inner class can access the outer class's members
so the inner class here can access the members the outer class inherited
from X
2)... also as it(the inner class) itself extends Y it has access to the members
of Y ..
so ultimately U have access to both the classes X and Y from the inner class
..
but the reverse is not always true .. ie U may not be able to access the
members of the inner class thru the outer class
Think this will suffice ..
and every one else in case of any errors do suggest the corrections
Rahul
-
Re: Multiple inheritance
"Travis" <trabis@hotmail.com> wrote:
>
> I like to know how to solve multiple interitance.
>
As you have created class A then you can make that extend Applet then you
would have access to getParameter() from both.
you could even redefine the method.
Hope it helps!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks