• No results found

Java Chapter-10

N/A
N/A
Protected

Academic year: 2020

Share "Java Chapter-10"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter-10

Interfaces: Multiple Inheritance

An interface is a special case of abstract class, which contains all the final variables and abstract methods (methods without their implementation). An interface specifies what a class must do, but not how to do.

Using the keyword interface, we can fully abstract a class interface from its implementation. Interfaces are syntactically similar to classes, but they lack instance variable, and their methods are declared without anybody. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.

Defining interface

An interface is defined much like a class. This is the general form of an interface:

access interface interface_name {

type final_varname1=value; type final_varname2=value; . . . .

Returntype method-name1(parameter_list); returntype method-name2(parameter_list); . . . .

}

Here, access is either public or not used. Interface_name can be any valid identifier. Methods which are declared have no bodies. They end with a semicolon after the parameter list. They are explicitly abstract methods. Variables are implicitly final and static, meaning they cannot be changed by the implementing class. They must be initialized with a constant value. All the methods and variable are implicitly public if the interface, itself is declared as public.

Example:

interface Item {

(2)

Note that the code for the method is not included in the interface and the method declaration simply ends with a semicolon. The class that implements this interface must define the code for the method.

Q. Explain the differences between class and interface

class Interface

The members of a class can be constant or variables.

The members of an interface are always declared as constant, i.e., their values are final.

The class definition can contain the code for each of its methods. That is, the methods can be abstract or non-abstract.

The methods in an interface are abstract in nature,i.e., there is no code associated with them. It is later defined by the class that implements the interface.

It can be instantiated by declaring objects. It cannot be used to declare objects. It can only implemented by a class.

It can use various access specifiers like public, private, or protected.

It can only use the public access specifier.

Extending Interfaces

Like classes, interfaces can also be extended. That is, an interface can be sub interfaced from other interfaces. The new sub interface will inherit all the members of the super interface in the manner similar to sub classes. This is achieved using the keyword extends as shown in below.

interface name2 extends name1 {

Body of name2; }

Consider the following example, interface ItemConstants

{

int code=1001; String name=”Fan”; }

interface Item extends ItemConstants {

(3)

class Display implements Item {

public void display() {

System.out.println(code + “ “ + name); }

}

Implementing Interfaces

Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the implements clause looks like this:

class class_name implements interface_name {

Body of class name }

Here the class class_name “implements” the interface interface_name.

(4)

The following program illustrates the concept of interfaces in java.

//InterfaceTest.java

interface Area //Interface defined {

final static float pi=3.14f; float compute(float x,float y); }

class Rectangle implements Area {

public float compute(float x, float y) {

return (x*y); }

}

class Circle implements Area {

public float compute(float x,float y) {

return (pi*x*y); }

}

class InterfaceTest {

public static void main(String args[]) {

Rectangle rect=new Rectangle(); Circle cir=new Circle();

Area area; area=rect;

System.out.println("Area of Rectangle=" + area.compute(10,20)); area=cir;

System.out.println("area of circle=" + area.compute(10,10)); }

(5)

Output:

Accessing Interface Variables

Interfaces can also be used to declare a set of constants that can be used in different classes. The constant values will be available to any class that implements the interface. The values can be used in any method, as part of any variable declaration, or anywhere where we can use a final value.

Example

interface A {

int m=10; int n=50; }

class B implements A {

void method(int size) {

---if(size<n) ---}

(6)

It is one in which it contains multiple base classes and a s ingle derived class. It is not possible through classes in java but it is possible through interfaces.

The following program illustrates the concept of implementing multiple inheritance in java.

Aim: Implementing the Multiple Inheritance in java

//Hybrid.java

//Program to illustrate the concept of Implementing the Multiple inheritance in java.

class Student {

int rno;

void getNumber(int n) {

rno=n; }

void putNumber() {

System.out.println("Roll No:" + rno); }

}

class Test extends Student {

float part1,part2;

void getMarks(float m1,float m2) {

part1=m1; part2=m2; }

void putMarks() {

System.out.println(" Marks Obtained"); System.out.println("Part1=" + part1); System.out.println("Part2=" + part2); }

}

interface Sports {

float sportWt=6.0f; void putWt(); }

class Results extends Test implements Sports {

float total;

(7)

System.out.println("Sports Wt= " + sportWt); }

void display() {

total=part1+part2+sportWt; putNumber();

putMarks(); putWt();

System.out.println("Total score= " + total); }

}

class Hybrid {

public static void main(String args[]) {

Results std1=new Results(); std1.getNumber(1234); std1.getMarks(27.5f,33.0f); std1.display();

} }

References

Related documents

innovation in payment systems, in particular the infrastructure used to operate payment systems, in the interests of service-users 3.. to ensure that payment systems

compass.init init (); (); // start accelerometer and magnetometer // start accelerometer and

For the poorest farmers in eastern India, then, the benefits of groundwater irrigation have come through three routes: in large part, through purchased pump irrigation and, in a

In this study, we take ad- vantage of the Zero-Determinant (ZD) strategy to analyze the block withholding attack between any two pools, where the ZD adopter has the unilateral

• On the one hand, we demonstrated at most a square or polynomial difference between the time complexity of problems measured on deterministic single tape and multi-tape

This is really a legal question, but the key seems to be that the first sentence of Clause 67.1 says that it continues after termination. A few additional comments may be helpful.

For event handling interfaces with more than one method, Java provides a corresponding class (adapter class) that already implements all the methods in the interface for you.. All