internal overshadowed class ?

V

vertigo

Hello
i have:
public class A
{
....
class AA {...}
function(){ AA a = new AA()}
}

and:
public class B extends A
{
//i want to have new class AA
class AA {...}
function(){ AA a = new AA()}
}

but compiler does not want to compile, because in class B it sees two
definition for AA class. I want to see in B class only new definition
of AA class (overshadow it). Is it possible ? (new AA class can not
inheritate from old AA class because it's quite different)

Thanx
Michal
 
T

Thomas Fritsch

vertigo said:
Hello
i have:
public class A
{
...
class AA {...}
function(){ AA a = new AA()}
}

and:
public class B extends A
{
//i want to have new class AA
class AA {...}
function(){ AA a = new AA()}
}

but compiler does not want to compile, because in class B it sees two
definition for AA class. I want to see in B class only new definition
of AA class (overshadow it). Is it possible ? (new AA class can not
inheritate from old AA class because it's quite different)

Thanx
Michal
Write
private class AA {...}
in both places, and the compiler should not complain anymore.
 
V

vertigo

Hello
i suppose i had not understood compiler error correctly.
i will describe once again with compiler errors:

public class A
{
...
class AA {...} extends Thread
function(){ AA a = new AA()}
}

and:
public class B extends A
{
//i want to have new class AA
class AA {...} extends Thread
function(){ AA a = new AA()}
}

and when i try to compile i receive:
B.java:88: incompatible types
found : B.AA
required: A.AA
AA object = new AA();
^
when i declare both AA classes as private i receive:
B.java:88: incompatible types
found : B.AA
required: A.AA
AA object = new AA();
^
B.java:89: start() in java.lang.Thread is not defined in a public cl
ass or interface; cannot be accessed from outside package
object.start();

Why ? Does it mean that class which extend Thread must be public to call
it's start() method ?

Thanx
Michal
 
V

vertigo

public class A
{
...
class AA {...} extends Thread
function(){ AA a = new AA()}
}

and:
public class B extends A
{
//i want to have new class AA
class AA {...} extends Thread
function(){ AA a = new AA()}
}

interesting thing is when from class B in function i call:
function(){ AA a = new A.AA()}
everything compiles fine, but i want to call:
function(){ AA a = new B.AA()}
but compiler claims that it needs A.AA.
Why ?
 
J

John C. Bollinger

vertigo said:
interesting thing is when from class B in function i call:
function(){ AA a = new A.AA()}
everything compiles fine, but i want to call:
function(){ AA a = new B.AA()}
but compiler claims that it needs A.AA.
Why ?

If you want some real answers then post some real code. Skeletons that
demonstrate the problem are fine, but your example is not even
particularly close to valid Java syntax. The following syntactically
correct Java compiles fine for me:

public class A {
class AA extends Thread {}
void function(){ AA a = new AA(); }
}

class B extends A {
class AA extends Thread {}
void function() { AA a = new AA(); }
}

Note that I do not even have to make the inner classes private, although
it works just as well if I do.

It is unlikely that your problem is a broken Java compiler, so I'll
assume that you're misrepresenting the structure of the classes you are
trying to write. As I said, post a real example that we can critique if
you want help.
 
T

Tony Morris

you can't just go putting code for a new class in the middle of a
method. It must go outside all methods, unless you use the special
syntax for anonymous classes.

Incorrect.
You can declare both named and anonymous classes as method-local.


// perfectly fine
class T
{
void m()
{
class C
{

}

C c = new C()
{

};
}
}

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Roedy Green

You can declare both named and anonymous classes as method-local.

Arrgh. Nearly always when someone does that it is an error. Now the
compiler can't even complain.

Perhaps a lint could say "class T local to method m only.
 
V

vertigo

ok, i'm sending real code, i cut some unimportant parts of code:
base class:
public class DCCServer
{
public ServerSocket ss;
public Socket s;
public ReceiveThread rec_thread;
public ListeningThread list_thread;
public DCCNode node;

public DCCServer(IrcClient irc, String command, DCCNode n)
{
node = n;
init();
}
class ListeningThread extends Thread
{
public ListeningThread()
{
}

public void run()
{
.....
}
}
class ReceiveThread extends Thread
{
public void run()
{
....
analize(s);
}
}
public void analize(String s)
{
}
public void init()
{
......
list_thread = new ListeningThread();
list_thread.start();
}
}/*end of base class*/

and extended class:
public class DCCSendServer extends DCCServer
{
public DCCSendNode n;

public DCCSendServer(IrcClient irc, String command, DCCSendNode n)
{
super(irc, command, n);
}
class ListeningThread extends Thread
{
public ListeningThread()
{
}

public void run()
{
.....
}
}
public void init()
{
....../*code here is different to the code in base class*/
list_thread = new ListeningThread();//line 132
list_thread.start();
}
public void analize(String s)
{
}
}/*end of extended class*/

and the compiler says:
javac *.java
DCCSendServer.java:132: incompatible types
found : highland.ppoj.DCCSendServer.ListeningThread
required: highland.ppoj.DCCServer.ListeningThread
list_thread = new ListeningThread();

Why ? How can i solve this problem ?

Thanx
Michal
 
V

vertigo

Hello
I finally solved the problem:
i forgot to add:
public ListeningThread list_thread;
to extended class.

Once againg thanx for your help:)
 

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,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top