The document discusses how to define an interface in Java that specifies methods for generating different types of number series. An interface named Series is defined with three methods - getNext() to return the next number, reset() to restart the series, and setStart() to set the starting value. Classes can then implement the Series interface and provide implementations of these three methods to generate specific types of number series, like one that returns even numbers starting from 2.
Roadmap to Membership of RICS - Pathways and Routes
Creating Interface- Practice Program 6.docx
1. Creating Interface
Core purpose of the interface is to specify what must be done.
Assume you want to create a set of classes that generate different types of number
series the series must be of even numbers begin with 2, random numbers or set of
prime numbers in all cases you have methods and those methods obtain the next
number in the series, need to reset the series to start, and need to specify starting value.
For Example:
public interface Series
{
int Void getNext( ); // Return next number in series
void reset( ); // restart
void setStart( int x); // set starting value
}
Now Series DEFINES THREE METHODS
getnext( ) which will obtain next number in the series
void reset( );which will reset the series to starting point.
void setStart( int x); which will used to set the starting point.
By calling the same set of methods the series will be declared as public and held in the
file called Serise.java
Ones interface has been defined one or more classes can implement the interface
To implement an interface two methods are used.
1) In a class declaration include an implement clause that specifies the interface is being
implemented
2) Instead in the class implement the methods defined by the interface.
2. Example:
public interface Series
{
int Void getNext( ); // Return next number in series
void reset( ); // restart
void setStart( int x); // set starting value
}
//Implement Series
Class ByTwos implements Series {
int start:
int val;
ByTwos( ) {
start=0;
val=0;
}
public int getNext( ) {
val+=2;
return val; }
void reset( ) {
val=reset;
}
void setStart( int x) {
start=x;
val=x;
}
}
// Demonstration of use of Series.
Class Series Demo {
public static void main(String[] args) {
ByTwos ob=New ByTwos();
for( int i=0;i<5;i++)
System.out.println(“Next Value is:” +ob.getNext());
System.out.println(“n Restarting:” );
Ob.reset( );
for( int i=0;i<5;i++)
System.out.println(“Next Value is:” +ob.getNext());
System.out.println(“n Starting at 100:” );
Ob.setStart(100);
for( int i=0;i<5;i++)
System.out.println(“Next Value is:” +ob.getNext());
}
}
Output:
3. Next Value is: 2
Next Value is: 4
Next Value is: 6
Next Value is: 8
Next Value is: 10
Resetting
Next Value is: 2
Next Value is: 4
Next Value is: 6
Next Value is: 8
Next Value is: 10
Starting at 100
Next Value is: 102
Next Value is: 104
Next Value is: 106
Next Value is: 108
Next Value is: 110
Example:2
interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
// DemoClass "implements" FirstInterface and SecondInterface
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}
class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
OUTPUT:
Some text...
Some other text...
4. Example 3:
interface Polygon {
void getArea(int length, int breadth);
}
// implement the Polygon interface
class Rectangle implements Polygon {
// implementation of abstract method
public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length * breadth));
}
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
Output:
The area of the rectangle is 30
Example 4:
interface Polygon {
void getArea();
// default method
default void getSides() {
System.out.println("I can get sides of a polygon.");
}
}
// implements the interface
class Rectangle implements Polygon {
public void getArea() {
int length = 6;
int breadth = 5;
int area = length * breadth;
System.out.println("The area of the rectangle is " + area);
}
// overrides the getSides()
public void getSides() {
System.out.println("I have 4 sides.");
5. }
}
// implements the interface
class Square implements Polygon {
public void getArea() {
int length = 5;
int area = length * length;
System.out.println("The area of the square is " + area);
}
}
class Main {
public static void main(String[] args) {
// create an object of Rectangle
Rectangle r1 = new Rectangle();
r1.getArea();
r1.getSides();
// create an object of Square
Square s1 = new Square();
s1.getArea();
s1.getSides();
}
}
Output:
The area of the rectangle is 30
I have 4 sides.
The area of the square is 25
I can get sides of a polygon.