Escolar Documentos
Profissional Documentos
Cultura Documentos
Lecture Objectives
Introduction to Inner Classes.
Gain an appreciation of the functionality of
inner classes.
Assess the different types of inner classes
Inspect how Java circumvents the issue of
multiple inheritance.
Introduction
Its possible to place a class definition
Example
class MyOuter {
private int x = 7;
// inner class definition
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
}
} // close inner class definition
} // close outer class
Instantiating an Inner
Class
To create an instance of an inner class, you
Example
class MyOuter {
private int x = 7;
public void makeInner() {
MyInner in = new MyInner(); // make an inner instance
in.seeOuter();
}
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
public static void main(String[] args) {
MyOuter mo = new MyOuter(); // gotta get an instance!
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
}
} } } What is the Output
of a method.
There are two types
Version One
class Gwadla{
public void pop(){
System.out.println(gwadla);
}
}
class Food {
Gwadla gw = new Gwadla{
public void pop(){
System.out.println(anonymous
gwadla);
}
Version 2
Here we create an anonoymous implementer
Example
interface Eatable{
public void eat();
}
Class Fruit{
Eatable e = new Eatable(){
public void eat {
System.out.println(anonymous
eatable implementer);
}
};
}
class BigOuter {
Static class Nest {
public void go(){
System.out.println(hello);}}
}
Class Broom{
static class B2 {
public void goB2(){
System.out.println(hello2);}}