0% found this document useful (0 votes)
39 views19 pages

Understanding Interfaces and Nested Classes

The document discusses interfaces and nested classes in Java. Interfaces define methods but provide no implementation, while classes implement interfaces by providing method bodies. Nested classes can be either static or non-static; static nested classes can access private members of the outer class while non-static nested classes are associated with outer class objects and have access to each object's methods and variables. The document provides examples of interfaces, implementing classes, and nested classes.

Uploaded by

ecardnyl25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views19 pages

Understanding Interfaces and Nested Classes

The document discusses interfaces and nested classes in Java. Interfaces define methods but provide no implementation, while classes implement interfaces by providing method bodies. Nested classes can be either static or non-static; static nested classes can access private members of the outer class while non-static nested classes are associated with outer class objects and have access to each object's methods and variables. The document provides examples of interfaces, implementing classes, and nested classes.

Uploaded by

ecardnyl25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Interfaces and Nested

Classes
September 19, 2014
Interfaces

CMSC 22
Interfaces

An interface consists of a set of instance


method interfaces, without any
associated implementations

A class can implement an interface by


providing an implementation for each of
the methods specified by the interface

CMSC 22
Interfaces

public interface Drawable {


public void draw(Graphics g);
}

CMSC 22
Interfaces

public class Line implements Drawable {


public void draw(Graphics g) {
. . . / / do something −− presumably ,
draw a l i n e
} . . . / / other methods and v a r i a b l e s
}
CMSC 22
Interfaces

To implement an interface, a class must


do more than simply provide an
implementation for each method in the
interface

CMSC 22
Interfaces

It must also state that it implements the


interface, using the reserved word
implements

CMSC 22
Interfaces

Any class that implements the Drawable


interface defines a draw() instance
method

Any object created from such a class


includes a draw() method

CMSC 22
Interfaces

While a class can extend only one other


class, it can implement any number of
interfaces

class FilledCircle extends Circle


implements Drawable, Fillable { . .
}
CMSC 22
Interfaces

The point of all this is that, although


interfaces are not classes, they are
something very similar

Though you can’t construct an object


from an interface, you can declare a
variable whose type is given by the
interface
CMSC 22
Nested Classes

A nested class is any class whose


definition is inside the definition of another
class

Nested classes can be either named or


anonymous

CMSC 22
Nested Classes

A named nested class, like most other


things that occur in classes, can be either
static or non-static

CMSC 22
Nested Classes

CMSC 22
Nested Classes

A static nested class has full access to the


static members of the containing class,
even to the private members

Similarly, the containing class has full


access to the members of the nested
class
CMSC 22
Nested Classes

Non-static nested classes are referred to


as inner classes

A non-static nested class is actually


associated with an object rather than to
the class in which it is nested

CMSC 22
Nested Classes

CMSC 22
Nested Classes

Each object that belongs to the


containing class has its own copy of the
nested class

CMSC 22
Nested Classes

This copy has access to all the instance


methods and instance variables of the
object, even to those that are declared
private

CMSC 22
Nested Classes

If the nested class needs to use any


instance variable or instance method,
make it non-static

Otherwise, it might as well be static

CMSC 22

You might also like