Why does we need Inner Class?

L

liupu

Hello:
Am I correct? And everyone's words is welcome. Thanks.


class A {
int i;
int geti() {
return i;
}
}
class B {
int j;
int getj() {
return j;
}
}
/* It's wrong to write as follows: */
/*
public class C extends A,B {
}
*/
/* But it's right as follow: */
public class C extends A{
class D extends B {

}
}
 
C

cp

We need inner classes for:

1) An object of an inner class can access the implementation of the object
that created it- INCLUDING private data.
2) Inner classes can be hidden for other classes in the same package.
3) Anonymous inner classes are very useful when you want to define callbacks
on the fly.
 
L

liupu

cp said:
We need inner classes for:

1) An object of an inner class can access the implementation of the object
that created it- INCLUDING private data.
2) Inner classes can be hidden for other classes in the same package.
3) Anonymous inner classes are very useful when you want to define callbacks
on the fly.
Thanks. I agree. But I think it did more than it looks.
 
C

Christopher Benson-Manica

liupu said:
/* It's wrong to write as follows: */
public class C extends A,B {
}

Yes, because the designers of Java, in their wisdom, decided to
prohibit multiple inheritance.
 
A

AndrewMcDonagh

Christopher said:
Yes, because the designers of Java, in their wisdom, decided to
prohibit multiple inheritance.

prohibit Multiple Implementation Inheritance, choosing Multiple
Interface Inheritance instead.
 
J

Jeffrey Schwab

AndrewMcDonagh said:
prohibit Multiple Implementation Inheritance, choosing Multiple
Interface Inheritance instead.

-1

Of all the features I miss in Java, multiple inheritance is probably
number two, right after template specialization. Number three is typedef.
 
L

liupu

Christopher said:
Yes, because the designers of Java, in their wisdom, decided to
prohibit multiple inheritance.

yes. The java designers prohibit multiple inheritance.
But in the case of "must multiple inheritance", I think the java
designers designs the Inner Class to do the same thing. And the Inner
Class did.
Right?
 
O

Oliver Wong

liupu said:
yes. The java designers prohibit multiple inheritance.
But in the case of "must multiple inheritance", I think the java
designers designs the Inner Class to do the same thing. And the Inner
Class did.
Right?

I don't really understand your question, but I think the design decision
of allowing inner class doesn't have much to do with multiple inheritance.

- Oliver
 
M

Mike Schilling

Oliver Wong said:
I don't really understand your question, but I think the design
decision of allowing inner class doesn't have much to do with multiple
inheritance.

IMHO, after Microsoft added the moral equivalent of method pointers to J++
and was soundly reprimanded by Sun for doing so, Sun was logically unable to
add method pointers itself, badly though they're needed for callbacks. Thus
inner classes were born.

See http://java.sun.com/docs/white/delegates.html for Sun's official take on
this, and decide for yourself how much is rationalization.
 
L

liupu

Thank you. I see.
Mike said:
IMHO, after Microsoft added the moral equivalent of method pointers to J++
and was soundly reprimanded by Sun for doing so, Sun was logically unable to
add method pointers itself, badly though they're needed for callbacks. Thus
inner classes were born.

See http://java.sun.com/docs/white/delegates.html for Sun's official take on
this, and decide for yourself how much is rationalization.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top