0% found this document useful (0 votes)
4 views6 pages

Implementing Interfaces in Java

The document explains how to implement interfaces in Java using the 'implements' keyword, detailing the requirements for method visibility and signature matching. It provides an example with the 'Series' interface and a 'ByTwos' class that implements it, demonstrating method functionality. Additionally, it includes a 'SeriesDemo' class that showcases the output of the 'ByTwos' class methods in action.

Uploaded by

mohanapriya.c
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Implementing Interfaces in Java

The document explains how to implement interfaces in Java using the 'implements' keyword, detailing the requirements for method visibility and signature matching. It provides an example with the 'Series' interface and a 'ByTwos' class that implements it, demonstrating method functionality. Additionally, it includes a 'SeriesDemo' class that showcases the output of the 'ByTwos' class methods in action.

Uploaded by

mohanapriya.c
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Implementing Interfaces in Java

With Example Implementation


Implementing Interfaces
• A class implements an interface using the
'implements' keyword.
• General form: class ClassName implements
InterfaceName { ... }
• To implement multiple interfaces, separate
them with commas.
• Implemented methods must be public.
• Method signatures must exactly match the
interface definition.
Interface: Series
• public interface Series {
• int getNext();
• void reset();
• void setStart(int x);
• }
Class ByTwos implements Series
• class ByTwos implements Series {
• int start; int val;
• ByTwos() { start = 0; val = 0; }
• public int getNext() { val += 2; return val; }
• public void reset() { val = start; }
• public void setStart(int x) { start = x; val = x; }
• }
Demonstrating ByTwos Class
• class SeriesDemo {
• public static void main(String args[]) {
• ByTwos ob = new ByTwos();
• for(int i=0; i<5; i++)
[Link]([Link]());
• [Link]();
• for(int i=0; i<5; i++)
[Link]([Link]());
• [Link](100);
Output of SeriesDemo
• 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

You might also like