Tuesday, 5 April 2011

what is interface

interface

Definition:

Interface is a pure abstract class that contains

public static final variables,

public Abstract methods

It provides only forcibility (specification) to subclass.

Need of interface:

Interface is invented in java for below two reasons

1) For releasing specification

2) For implementing multiple inheritance

Syntax:

[public/] interface

{

public static final data-type variable-name=variable value;

---------- -----------------------------------------------------------------;

----------------------------------------------------------------------------;

public abstract return-type method-name(arguments list…);

---------- -----------------------------------------------------------------;

----------------------------------------------------------------------------;

}

Creating interface

Using keyword “interface” programmers can develop a class of type interface.

We can create public and default interface, but we can’t create private and protected interfaces

The default access specifier of interface is

Before interface-name we can place “abstract” keyword in our .java file but in compilation phase compiler removes abstract keyword and generates .class file

Variables in interfaces

In interface by default all variables are public static final variables.

Public – because it as to access from ay where.

Static – because we can’t instance interface.

Final – because it is common value for all subclases

Since interface variables are static final, it must be initialized else it leads to

CE symbol “=” expected

Methods in interfaces

In interface by default all methods are public and abstract methods

since methods has to access from anywhere outside the class.

By default interface members access specifier is public

/*this is our written code means InterfaceDemo1.java file*/

interface InterfaceDemo1

{

public static final String shape = "Quadrilateral";

int sides = 4;

public abstract double areaOfQuadrilateral();

double perimeterOfQuadrilateral();

}

/*this is the compiler changed code means InterfaceDemo1.class file*/

interface InterfaceDemo1

{

public abstract double areaOfQuadrilateral();

public abstract double perimeterOfQuadrilateral();

public static final String shape = "Quadrilateral";

public static final int sides = 4;

}

/*

here we can see in variable declaration missing keywords are automatically

placed by compiler.

and interface access specifier "public" keyword not placed,

interface default access specifier is

*/

/*

We can compile interface but we can’t execute

because it doesn’t contain main method.

*/

Rules on interface

Do

Don’t

For interface either directly or indirectly can't be instantiated (creating objects). because it doesn't have a constructor. Violation leads to CompiletimeError CE:InterfaceDemo1 is abstract can't be instantiated.

But we can create reference variables of interface to store either null or its subclass object reference

In interface we can’t write concrete methods, including constructors and static and non static blocks. Violation leads to CE: interface method can’t have a body.

Inheritance with interface

We must use “implement” keyword to establish inheritance relation between class and interface

Rule on interface subclass

Subclass must implement all abstract methods of interface, else subclass should be declare as abstract otherwise it leads to CompiletimeError

These implementing methods access specifier in subclass should also be public, because interface methods are public methods.

Developing inheritance with two interface

we should use “extends” keyword to establish inheritance relation between two interfaces

Multiple inheritance using interfaces

Extending super class and implements interfaces

Extending interfaces

Difference between Interface and Abstract class

what is abstraction

Abstraction

Defination:

The process of providing necessary information (method proto-type ) by removing / hiding unnecessary information (method body) is called Abstraction.

Hiding implementation

Removing implementation

In first case (hiding implementation) abstraction provides reusability to the subclass developer.

As a superclass developer we define a method with logic and inform to user (subclass developer) only prototype, for using this method implementation is unnecessary for subclass developer.

EX

System.out.println();

Here we don’t bother about println() method implementation

All classes by default contain concrete methods hence they are implementing abstraction at first level.

in second case (removing implementation) abstraction provides forcibility to the subclass developer.

we should explicitly implement abstraction at second level

it means in super class we define a method without body, the method should be overridden and implemented in subclass based on subclass business requirement.

below steps shows us, implementing abstraction at second level for sub classes to implement sum logic.

1. Create abstract class by using abstract keyword.

Syntax

abstract class

Ex

public abstract MyAbstract{

2. Declare abstract method in that class , remove body of the method({}) And declare it as abstract method, by using abstract keyword(modifier)

Syntax

abstract(parameterlist);

EX

abstract void m1();

3. Define subclass from this abstract class and override abstract method in the subclass with that class business requirement

Rules in creating abstract class

1 it should contain abstract modifier in its prototype and should not have body else it leads to compile time error.

2 if a class contain abstract method it should be declared as abstract else it leads to compile time error.

Rules in implementing abstraction

Rule 1

abstract method must contain abstract keyword else it leads to compile time error missing method body or declare abstract.

Ex

void add();// CE

Abstract void add();

Rule 2

if a class contain abstract method it should be declared as abstract class using abstract keyword else it leads to compile time error.

Ex

Class Example{

Abstract void add ();//CE

}

Abstract class Example {

Abstraction void m1();

}

Rule 3

If a class is declared as abstract it can’t be instantiated directly means object can’t be create by using its constructor violation leads to CE

Ex

abstract class Example {

abstraction void m1();

p s v m (String[] args){

Example e= new Example();//CE: Example is abstract can’t be instantiated

}

Rule 4

Sub class must override all abstract methods of its super calss else it should be declared as abstract violation leads to compile time error

EX

class Sample extends Example{}//CE sample is not a abstract and doesnot override abstract method m1 in Example.

To solve the above CE we can give below 2 solutions

1) Declare sample class as abstract

2) Declare body for m1() in sample which is abstract method in Example