Page 472 - Computer science 868 Class 12
P. 472
12.5 INTERFACE IN JAVA
Java does not support multiple inheritance. In order to implement the same, we use the concept of interface. Interface
in Java is a blue print of a class as it specifies what to do, but does not specify how to do. It is a mechanism to achieve
abstraction by hiding certain features and revealing only some important details.
The following are the properties of an Interface.
• Interface in Java is declared by the keyword Interface
• The methods are all public abstract methods. However, Java 8 edition also allows default and static methods in interface.
• The data members are public, static final by default.
• A derived class can implement multiple interfaces. However, it must implement all it’s abstract methods.
• Interface cannot be instantiated.
• An interface has no constructors as it cannot create objects.
Implements Keyword
The abstract methods of interface are implemented by the derived class using the keyword implements.
Let us do a simple programs to understand this concept well.
Program 10 The interfaces Circle and Square are defined as follows:
Interface name : Square
Data Member
int s : Stores the length of the side
Member Methods
void sqArea() : Calculates and prints the area of square
Interface name : Circle
Data Member
double r : Stores radius
Member Methods
void cirArea() : Calculates and prints the area of circle
A derived class Area is defined to calculate and print the areas of square and circle as follows:
Class name : Area
Member Methods
void cirArea() : Calculates and prints the area of circle
void sqArea() : Calculates and prints the area of square
1 interface Square
2 { public static final int s=15;
3 public void sqArea();
4 }
5 interface Circle
6 { public static final double r=7.5;
7 public void cirArea();
8 }
9 class Area implements Square,Circle // multiple inheritance
470470 Touchpad Computer Science-XII

