[go: up one dir, main page]

0% found this document useful (0 votes)
14 views8 pages

OOPJ Notes 2 2

This document provides an overview of interfaces in Java, including their definition, implementation, and various features such as nested interfaces, default methods, and static methods. It explains the reasons for using interfaces, how to define and implement them, and the concept of extending interfaces. Additionally, it includes code examples to illustrate the concepts discussed.

Uploaded by

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

OOPJ Notes 2 2

This document provides an overview of interfaces in Java, including their definition, implementation, and various features such as nested interfaces, default methods, and static methods. It explains the reasons for using interfaces, how to define and implement them, and the concept of extending interfaces. Additionally, it includes code examples to illustrate the concepts discussed.

Uploaded by

khaleel2791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT - II

CHAPTER -2
INTERFACES
Contents
 Introduction to interfaces
 Defining an Interface
 Implementing interfaces
 Nested intefaces
 Variables in interfaces
 Extending interfaces
 Default Interface Methods
 Static Methods in an Interface
 References
INTRODUCTION TO INTERFACES
Interface: In Java, an interface is a blueprint of a class. It has abstract methods and static final
variables.

 It cannot be instantiated just like the abstract class


 Since Java 8, default and static methods can be added in an interface
 Since Java 9, private methods can be added in an interface
Reasons to use Interfaces in Java
There are mainly three reasons to use interface
o It is used to achieve abstraction
o By interface, multiple inheritance can be supported
o It can be used to achieve loose coupling
 Interfaces are designed to support dynamic dispatch method resolution at runtime
 Once an interface is designed, any number of classes can implement it and one class can
implement any number of interfaces
 There can be no default implementation of any method specified within an interface
 Each class that includes an interface must implement all of the methods
 Variables are implicitly static and final

Defining an interface
General form of an inheritance

access_modifier interface name{


returnType method1(parameter list);
returnType method2(parameter list);
type variable = value;
}

access_modifier can be either public or protected.

Implementing interfaces
access_modifier class implements intefaceName{
interface_method1(parameter list){
}
Interface_method2(parameter list){
}
}
 If a class implements more than one interface, the interfaces are separated by (,)

access_modifier class implements inteface1, interface2{


interface1_method(parameter list){
}
Interface2_method(parameter list){
}
}

 If a class implements two interfaces that declare the same method, then the same
method will be used by clients of either interface.
 The methods that implement an interface must be declared public.
/* Program implementing interface*/

interface GeoShapes{
double PI=22/7;
double area();
}
class Rectangle3 implements GeoShapes{
double dim1,dim2;
Rectangle3(double x, double y){
dim1=x;
dim2=y;
}
public double area() {
return dim1*dim2;
}
}
class Circle1 implements GeoShapes{
double dim1;
Circle1(double x){
dim1=x;
}
public double area() {
return dim1*dim1*PI;
}
}
public class InterfaceDemo {
public static void main(String[] args) {
GeoShapes r=new Rectangle3(10,20);
System.out.println("Area of Rectangle: "+r.area());
GeoShapes c= new Circle1(10);
System.out.println("Area of Circle: "+c.area());
}
}
Accessing Implementations through interface references:
 Variables can be declared for interfaces to refer the implementing class object
 When a method is called through one of the references, the correct version will be
called based on the actual instance of the interface being referred to.

Nested interfaces
 In Java, an interface can be defined within another class or interface.
 The interface that defined inside another class or interface is known as nested interface.
 The nested interface is also known as inner interface.
 The nested interface declared within an interface is public by default.
 The nested interface declared within a class can be with any access modifier.
 Every nested interface is static by default.
 The nested interface can be accessed by using outer interface or outer classname
followed by (.).
/* program implementing nested interface inside another interface */

interface OuterInterface{
void outerMethod();
interface InnerInterface{
void innerMethod();
}
}
class Child implements OuterInterface,OuterInterface.InnerInterface{
public void outerMethod() {
System.out.println("Outer method: ");
}
public void innerMethod() {
System.out.println("inner method:");
}
}
public class NestedInterface {
public static void main(String[] args) {
Child ob=new Child();
ob.outerMethod();
ob.innerMethod();
}
}
/* program implementing nested interface inside a class */

class OuterClass{
void outerMethod () {System.out.println("In OuterClass..");}
interface InnerInterface{
void innerMethod();
}
}
class Child extends OuterClass implements OuterClass.InnerInterface {
public void outerMethod() {
System.out.println("Outer method: ");
}
public void innerMethod() {
System.out.println("inner method:");
}
}
public class NestedInteface {
public static void main(String[] args) {
OuterClass ab = new Child();
ab. outerMethod ();
Child ob=new Child();
ob.outerMethod();
ob.innerMethod();
}
}

Variables in Interfaces
 Interfaces can be used to share constants by importing into multiple classes.

Interface SharedConstants{
String ODD=”ODD NUMBER”;
String EVEN=”EVEN NUMBER”;
}
Class EVENODD implements SharedConstants{
public static void main(String[] args) {
String res;
int x=5;
if(x%2 == 0) { res = EVEN;}
else {res = ODD; }
System.out.println(res);
}
}
Extending Intefaces
 One interface can inherit another interface by use of the keyword extends.
 When a class implements an interface that inherits another interface, it must provide
implementations for all methods defined within the interface inheritance chain.

/* program implementing interface extending*/

Interface ParentInterface{
void parentMethod();
}
Interface ChildInterface extends ParentInterface{
void childMethod();
}
class ImpClass implements ChildInterface{
Public void parentMethod(){
System.out.println(“In Parent Method”);
}
Public void childMethod(){
System.out.println(“In Child Method”);
}
}
class Demo{
public static void main(String args[]){
ImpClass ob = new ImpClass();
ob.parentMethod();
ob.childMethod();
}
}
Default Interface Methods
A default interface lets user to define default implementation for an interface method.
Purpose of Default Methods:
1. Interfaces could be expanded without breaking existing code
2. To specify methods in an interface that are essentially optional depending on how the
interface is used.
An interface default method is defined similar to a method defined in a class. The prmary
difference is that the declaration is preceded by the keyword default.

interface Stack{
void push(int item);
int pop();
// it need not be implemented by preexisting classes those implements Stack
default void clear(){
System.out.println(“clear method not implemented”);
}
}

Static Methods in an Interface


 JDK 8 added another new capability to interface: the ability to define one or more static
methods.
 Like static methods in a class, a static method defined by an interface can be called
independently of an object.
interface Interface1{
default void print() {
System.out.println("Nothing to print");
}
static void display() {
System.out.println("Display the details");
}
}
public class StaticAndDefaultInterface implements Interface1{
public static void main(String[] args) {
StaticAndDefaultInterface child=new StaticAndDefaultInterface();
child.print();
Interface1.display();
}
}
References:
1. Herbert Schildt, Java: The Complete Reference, 10thEdition, McGraw Hill
Education (India) Pvt. Ltd.
2. Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)
(oracle.com)
3. https://www.javatpoint.com/
4. http://www.btechsmartclass.com/java/java-tutorials.html

You might also like