SlideShare a Scribd company logo
1 of 5
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.
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:
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...
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.");
}
}
// 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.

More Related Content

Similar to Creating Interface- Practice Program 6.docx

KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
Aiman Hud
 
Modify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdfModify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdf
hullibergerr25980
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdf
dbrienmhompsonkath75
 
Programimport java.util.Scanner; public class AreaOfRectangle {.pdf
Programimport java.util.Scanner; public class AreaOfRectangle {.pdfProgramimport java.util.Scanner; public class AreaOfRectangle {.pdf
Programimport java.util.Scanner; public class AreaOfRectangle {.pdf
himanshukausik409
 
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdfRooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
mumnesh
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
Palak Sanghani
 

Similar to Creating Interface- Practice Program 6.docx (20)

unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
 
Java interface
Java interfaceJava interface
Java interface
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
Java programs
Java programsJava programs
Java programs
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Class 5 2ciclo
Class 5 2cicloClass 5 2ciclo
Class 5 2ciclo
 
Modify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdfModify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdf
 
Programimport java.util.Scanner; public class AreaOfRectangle {.pdf
Programimport java.util.Scanner; public class AreaOfRectangle {.pdfProgramimport java.util.Scanner; public class AreaOfRectangle {.pdf
Programimport java.util.Scanner; public class AreaOfRectangle {.pdf
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdfRooms and MoreCan you please help me the JAVA programLabInherit.pdf
Rooms and MoreCan you please help me the JAVA programLabInherit.pdf
 
Computer programming 2 chapter 1-lesson 2
Computer programming 2  chapter 1-lesson 2Computer programming 2  chapter 1-lesson 2
Computer programming 2 chapter 1-lesson 2
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 
Java programs
Java programsJava programs
Java programs
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 

More from R.K.College of engg & Tech (15)

Module 5(Matplotlib and tkinter).pdf
Module 5(Matplotlib and tkinter).pdfModule 5(Matplotlib and tkinter).pdf
Module 5(Matplotlib and tkinter).pdf
 
Module 5(Numpy).pdf
Module 5(Numpy).pdfModule 5(Numpy).pdf
Module 5(Numpy).pdf
 
Module 5(Pandas).pdf
Module 5(Pandas).pdfModule 5(Pandas).pdf
Module 5(Pandas).pdf
 
Module IV_updated(old).pdf
Module IV_updated(old).pdfModule IV_updated(old).pdf
Module IV_updated(old).pdf
 
Module III.pdf
Module III.pdfModule III.pdf
Module III.pdf
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Python_Module_1.pdf
Python_Module_1.pdfPython_Module_1.pdf
Python_Module_1.pdf
 
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-8.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-4.pdf
Ch-4.pdfCh-4.pdf
Ch-4.pdf
 
Ch-3.pdf
Ch-3.pdfCh-3.pdf
Ch-3.pdf
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 

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.