Você está na página 1de 31

Advanced OOP Features

• Java Modifiers
• Abstract Classes
• Interface
• Inner Class
• Classes Inside a Method
• Anonymous Inner Classes
Java Modifiers
Access Modifiers
• public
• private
• protected
Other Modifiers
• static
• final
Access Modifiers
public
- class, attributes and methods which
have been declared as public can be
accessed/used by any class without
restriction
private
-can only be applied to attributes and
methods and access to them is
restricted to the class where they are
defined
Access Modifiers
protected
-can only be applied to attributes and methods
- access to the attributes and methods are
allowed for subclasses and other classes in the
same package
“default “ or friendly
-if no access modifier is specified, the modifier is
assumed to be friendly
- can be applied to class, attributes and methods
and access to them is allowed for classes
within the same package only
Static Modifier
- can be applied to attributes, methods,
code blocks and inner classes
static variables or data members
- referred to as class members
- shared by all objects of the class
- can be accessed without creating an
object
Static Modifier
static method
- referred to as class methods
- methods that refer to static members
or methods that do not refer to any
particular object
- no this pointer
- can be invoked without creating an
object of a class
Static Modifier
static initializer
- static code block that is executed only
once when the class is loaded
- normally used to initialize static
variables
final Modifier
- can be applied to classes, variables,
methods
- final class can not be subclassed
- a final method can not be overridden
- a final variable is a constant
Abstract Class
- As you move up to the inheritance hierarchy, classes become
more general and probably more abstract

Vehicle

Car Motorcycle Truck

Sedan Wagon

- At some point, the ancestor class is so general that it is more of


a framework for other classes than a class with specific instanc

- Abstraction makes your code cleaner.


Abstract Class
In the diagram, suppose all instruments have a common method,
namely play();
a. Flute - play a flute
b. Guitar - play a guitar
c. Harmonica - play a harmonica

How do you implement play() in the parent Instrument class.


public abstract class Instrument {
private String name;
public Instrument(String name) {
this.name = name;
}
public abstract void play();
}
Abstract Class
The abstract class promises that all non-abstract descendants of
this class will implement the abstract methods.

An abstract class cannot be instantiated. You have to extend that


class to a non-abstract class in order to create objects of that cl

You can create reference variables of an abstract class, but these


variables must refer to an object of a non-abstract subclass.

Instrument ins = new Flute(“Flute”);


Abstract Class (continued)
llegal instantiation :

Instrument ins = new Instrument(“Flute”);

- An abstract class can be subclassed to another abstract class.


This is possible if the subclass does not implement all abstract
method of its parent.
nterface
- if all the methods of a class are abstract, then it is better to make
it as an interface
- is the Java’s way of reflecting the behavior of two or more parent
(multiple inheritance).
- Java can only have one superclass. C++ allow a class to have
more than one superclass.

- Interface defines :
a. “classes” with pure abstract methods and no attribute
definitions.
b. Methods that every interface implementation must implemen
C. no actual implementation of methods.

- the keyword implements is used by all interface implementors.


Interfaces
•Declared types in Java are either classes or
interfaces
•Interfaces represent a promise of supported
services of the objects which implement the
interface – a “contract”
•An interface is simply a list of method declarations
–Its methods are declared but not implemented
–An interface is not a class, although it looks
remarkably like an abstract class
public interface File {
public void open(String name);
public void close();
}
Protocols
•An interface defines a protocol (a set of methods)
–If a class implements a given interface, then that class implements
that protocol
•An interface can be used to impose a common protocol on a
group of classes that are not related by inheritance.
–In the chain of classes related by inheritance the common protocol
is imposed through subclassing
Implementing Interface Methods
•Methods declared in an interface are implemented
in the classes which support that interface
public class TextFile implements File
{
public void open(String name) {
// implementation of open method
}
public void close() {
// implementation of close method
}
}
Typing and Interfaces
•A variable's type can be an interface
–Only objects whose class implements that
interface can be bound to that variable
–Only messages defined by the interface can
be used
–Interfaces cannot appear in a new expression
File r = new File(); // Error
File f = new TextFile(); // OK!
Subinterfaces
•Interfaces can be extended
–Interfaces are not classes so this hierarchy is
independent of the class hierarchy
–The interface which extends another interface
inherits all of its method declarations
interface File {
public void open(String name);
public void close();
}
interface ReadableFile extends File {
public byte readByte();
}
interface WritableFile extends File {
public void writeByte(byte b);
}
interface ReadWriteFile extends ReadableFile,
WritableFile {
public void seek(int position);
}
Naming Conventions for Interfaces
•Make "able" interfaces
–Cloneable, Serializable, ...
•Name interfaces using proper nouns and provide "Impl"
implementation of your interfaces
–Bank, BankImpl, BankAccount, BankAccountImpl
–With this convention, the interface typically contains a
definition for all (or most) of the implementation class'
public methods
•Prefix interface names with "I" and use proper nouns for your
classes
–IBank, Bank, IBankAccount, BankAccount
nner Class

- A class can be declared in any scope. Classes defined in other


classes, including those defined in methods, are called inner
classes.
public class OuterClass {
private int x = 1;
public class InnerClass {
private int y = 2;
public void InnerMethod() {
System.out.println(“x=“+x);
System.out.println(“y=“+y);
}
}
public void OuterMethod() {
System.out.println(“x=“+x);
}
}
Inner Class
Initialization of inner class :

public static void main (String[] args) {


OuterClass.InnerClass i = new OuterClass.new InnerClass();
i.innerMethod();
}

Or :

public static void main (String[] args) {


OuterClass o = new OuterClass();
OuterClass.InnerClass i = o.new InnerClass();
i.innerMethod();
}
Static Inner Class

- Java’s inner class mechanism allows an inner class to be


marked
static.

- A static inner class is associated with the class, rather


than the
instance of the class.

- A static inner class does not have reference to the


enclosing
variables.

- However static inner class can have references to the


enlosing
outer class static variables.
public class StaticOuter {
static int x =1;
int y = 2;
static class StaticInner{
int z = 3;
public void innerMethod() {
System.out.println(“x=“+x);
//System.out.println(“y=“+y);
System.out.println(“z=“+z);
}
}
}
Static Inner Class

public class Test {


public static void main (String[] args) {
System.out.println(“z “ + StaticOuterOne.StaticInnerONe.z);
}
}

- A static inner class behaves just like a top level class with
modified naming scheme.
Classes Inside a Method

- An inner class inside a method has access to any variable (local


parameters) of the outer class provided that it is final.
public class MOuter {
public void go (int x, final int y) {
int a = x + y;
final int b = x - y;
class MInner {
int k;
public void method() {
System.out.println(y);
System.out.println(b);
System.out.println(k);
}
}
}
}
Classes Inside a Method

- The class can be instantiated inside the method like an ordinary


class :

public void go (int x, final int y) {


class MethodInner {
public void method() {
System.out.println(y);
}
}
MethodInner mI = new MethodInner();
mI.method();
}
Anonymous Inner Classes

- classes without a name that is defined inside a method


- defined in place since it cannot be instantiated with new

Button b = new Button(“Ok”);


b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// write code to handle the event here
}
} );

Você também pode gostar