15 interfaces

Post on 10-Nov-2014

816 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

InterfacesDhrubojyoti Kayal

Some classes are better of just defining common behavior

You also want to prevent possibility of creating instances of this class

Make the class abstractpublic class Shape{

public abstract void draw() {}

}

Abstract class

A class containing abstract methods is called an abstract class.

If a class contains one or more abstract methods, the class itself must be qualified as abstract

public abstract class Shape {public abstract void draw();

}

Abstract Class

You cannot create an object of a class declared abstract If you inherit from an abstract class and you want to make

objects of the new type, you must provide method definitions for all the abstract methods in the base class

If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword.

It’s possible to make a class abstract without including any abstract methods

You can have methods in an abstract class which have reusable code and they themselves are not declared abstract

Abstract Class

Create an abstract Java class with one abstract method and another non abstract method

Try to create an instance of the abstract class

Excerise

The interface keyword produces a completely abstract class, one that provides no implementation at all.

It allows the creator to determine method names, argument lists, and return types, but no method bodies.

An interface provides only a form, but no implementation. An interface says, "All classes that implement this

particular interface will look like this." Any code that uses a particular interface knows what

methods might be called for that interface, and that’s all. So the interface is used to establish a "protocol" between

classes

Interfaces

To create an interface, use the interface keyword instead of the class keyword

Same access specifiers work as with classes, but mostly public

An interface can also contain fields, but these are implicitly static and final.

To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword

Interfaces

public interface Singable {public void sing();

}

public class Parrot implements Singable {public void sing() {}public void fly() {}

}

Interface in Action

Design an interface to ensure that Shape class and its children– Rectangle, Square can be drawn

Design an interface to ensure that Shape class and its children – Rectangle, Square can be filled with color

Now write a Test class and create two references one each of the children and assign them to a Shape variable. Invoke the different methods

Now try assigning the Shape references to the interface variables. Try invoking the methods

Exercise

Just like class it is possible to extend an interface

public interface A {public int a();

}public interface B extends a {

}

Extending an interface

Q&A

top related