SlideShare a Scribd company logo
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

unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
sujatha629799
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
Java Generics
Java GenericsJava Generics
Java Generics
jeslie
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
Ganesh Samarthyam
 
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
 
Java programs
Java programsJava 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
Footageetoffe16
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
Ahmad sohail Kakar
 
Class 5 2ciclo
Class 5 2cicloClass 5 2ciclo
Class 5 2ciclo
Carlos Alcivar
 
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
 
Inheritance
InheritanceInheritance
Inheritance
Mavoori Soshmitha
 
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
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
MLG College of Learning, Inc
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
PavanKumar823345
 
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
 
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
MLG College of Learning, Inc
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
Aram Mohammed
 
Java programs
Java programsJava programs
Java programs
Mukund Gandrakota
 
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

Module 5(Matplotlib and tkinter).pdf
Module 5(Matplotlib and tkinter).pdfModule 5(Matplotlib and tkinter).pdf
Module 5(Matplotlib and tkinter).pdf
R.K.College of engg & Tech
 
Module 5(Numpy).pdf
Module 5(Numpy).pdfModule 5(Numpy).pdf
Module 5(Numpy).pdf
R.K.College of engg & Tech
 
Module IV_updated(old).pdf
Module IV_updated(old).pdfModule IV_updated(old).pdf
Module IV_updated(old).pdf
R.K.College of engg & Tech
 
Module III.pdf
Module III.pdfModule III.pdf
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
R.K.College of engg & Tech
 
Python_Module_1.pdf
Python_Module_1.pdfPython_Module_1.pdf
Python_Module_1.pdf
R.K.College of engg & Tech
 
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...
R.K.College of engg & Tech
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-4.pdf
Ch-4.pdfCh-4.pdf
Ch-3.pdf
Ch-3.pdfCh-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
R.K.College of engg & Tech
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
R.K.College of engg & Tech
 

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

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 

Recently uploaded (20)

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 

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.