problem

A

Andersen

I have a class A and a class B inheriting from A.

I have a class C and a class D inheriting from C.

I would like to have a attribute ATTR of type C in A. But in B, I would
ATTR to be of type D.

What should I do?

regards,
Andersen
 
M

Michael Rauscher

Andersen said:
I have a class A and a class B inheriting from A.

I have a class C and a class D inheriting from C.

I would like to have a attribute ATTR of type C in A. But in B, I would
ATTR to be of type D.

What should I do?

Given your description: no problem.

class A {
private C attr;

public void doSomething() {
c = new C();
// ...
}
}

class B extends A {
private D attr;

public void doSomething() {
d = new D();
// ...
}


Bye
Michael
 
X

xarax

Michael Rauscher said:
Given your description: no problem.

class A {
private C attr;

public void doSomething() {
c = new C();
// ...
}
}

class B extends A {
private D attr;

public void doSomething() {
d = new D();
// ...
}

public class C
{
}

public class D
extends C
{
}

public class A
{
protected final C attr;

public A(final C theAttr)
{
attr = theAttr;
}

public C getAttr()
{
return attr;
}
}

public class B
extends A
{
public B(final D theAttr)
{
super(theAttr);
}

/* In J2SE 5.0 ONLY! Covariant return type. */
public D getAttr()
{
return (D) attr;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top