15 interfaces

11
Interfaces Dhrubojyoti Kayal

Upload: dhrubo-kayal

Post on 10-Nov-2014

816 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 15   interfaces

InterfacesDhrubojyoti Kayal

Page 2: 15   interfaces

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

Page 3: 15   interfaces

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

Page 4: 15   interfaces

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

Page 5: 15   interfaces

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

Try to create an instance of the abstract class

Excerise

Page 6: 15   interfaces

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

Page 7: 15   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

Page 8: 15   interfaces

public interface Singable {public void sing();

}

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

}

Interface in Action

Page 9: 15   interfaces

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

Page 10: 15   interfaces

Just like class it is possible to extend an interface

public interface A {public int a();

}public interface B extends a {

}

Extending an interface

Page 11: 15   interfaces

Q&A