0% found this document useful (0 votes)
21 views7 pages

04 Interface

The document discusses Java interfaces, including defining interfaces, extending interfaces, implementing interfaces, and comparing interfaces to classes. Interfaces in Java allow for multiple inheritance and defining common behaviors without implementation that classes can then use.
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)
21 views7 pages

04 Interface

The document discusses Java interfaces, including defining interfaces, extending interfaces, implementing interfaces, and comparing interfaces to classes. Interfaces in Java allow for multiple inheritance and defining common behaviors without implementation that classes can then use.
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/ 7

JAVA PROGRAMMING

INTERFACE.

 Interface
 Define Interface, implementing interface,
 Accessing interface, variables & methods,
 Extending interfaces,
 Interface references,
 Nested interfaces

INTERFACE
As we know about classes and how they can inherited by other classes.
Multiple Inheritances is Not Supported by Java through Classes.
 Use of multiple inheritance (or Hybrid Inheritance) proves difficult and adds complexity to the language so
Java does not support multiple inheritance.
 That is, classes in Java can not have more than one super class.
 If you try to give the definition like
o class A extends B, extends C {….} or
o class A extends B,C
 The extention of multiple classes is not permitted in Java.
New Approach of Multiple Inheritances in Java.
 Java provides an alternate approach known as interfaces to support the concept of multiple inheritances.
 A large number of real life applications require the use of multiple inheritance, through which we inherit
methods and properties from several distinct classes.
 Though multiple inheritance is not supported by java directly but it is implemented by the use of interface.

About The Interface?


 Interface is similar to the class in java.
 Interface is a keyword, allows to create user define data type like class.
 Interface is collection of data members and methods.
o The data members (fields) declare in interface are, by default public static and final. So they must
initialize with some constant value.
o All methods declare in interface are, by default public and abstract. This means that interface do not
specify any code to implement those methods.
 We can’t declare abstract constructor.
 We can’t create object of interface but we can create it’s reference.
 One interface can extends another interface but it can’t extends class.
 In Java any classes cannot have more than one super class.

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 1 of 7


 Interfaces is implemented by one class or more classes.
 Interface methods must be override with public access in a class in which it implements.

Need Of Interface
 Java allow have more than one super class in the form of interface.
 To achieve multiple Inheritance.
 We can implement one or more Interface(s) in the one class.
 Methods can be implemented by one or more class(s).

Features of Interface
 The Variable of an interface :
o are explicitly declared final and static (as constant).
o The class which implements interface, cannot change them.
o They must be initialize with a constant value.
o All the variable are implicitly public of the interface.
 The Method declaration of :
o Interface contains only a list of methods without any statement.
o Methods declaration terminated with a semicolon.
o The method are by default abstract.
o So the classes which implements the interface, must override all the methods of interface with public
access.
HOW TO ACHIEVE MULTIPLE INHERITANCE WITH INTERFACE.
 It is a type of inheritance where a derived class may have more than one parent class.
 It is not possible in case of java as you cannot have two classes at the parent level Instead there can be one
class and many interfaces at parent level to achieve multiple interface.
 Interface is similar to classes but can contain on final variables and abstract method.
 Interfaces can be implemented to a derived class.
 Syntax
 Example

DEFINING AN INTERFACE:
 Defining an interface is just like defining a class, except that the class keyword is replaced with the interface
keyword, and abstract methods and constant fields (data members) are allowed in an interface.
 Every interface is by default abstract, all it’s methods are by default declared as abstract and public. So you do
not need to explicitly put the abstract or public modifiers before them.
 Similarly, all the data members declared in an interface are by default public static and final. They must be
initialize with the constant value. We do not need to explicitly insert the final, static or public modifiers before
them.

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 2 of 7


 Here is an example of an interface definition.
 Syntax:
Access interface InterfaceName
{
[public static final] data_type variable1 = value1; // data for interface
[public static final] data_type variable2 = value2; // data for interface
[public] return_type method_name1(parameter list); //abstract method
[public] return_type method_name2(parameter list); //abstract method
}
 Example:
public interface Trainee
{
int stipend = 3000; // minimum monthly salary
int lectures = 18; // no of weekly lecture allotted
void showInfo(); // To print the Working information of Trainee
}
Note that the code for method showInfo() is not included in the interface.
The method declaration ends with a semicolon.
The class that implements this interface must define code for this (showInfo()) method.

FEATURES OF INTERFACE:
 Related To Data Members.
o Variable of an interface are explicitly declared final and static (as constant) meaning that the
implementing the class cannot change them.
o They must be initialize with a constant value.
o All the variable are implicitly public of the interface, itself, is declared as a public
 Related To Methods.
o Method declaration contains only a list of methods without any body statement and ends with a
semicolon.
o The method are, essentially, abstract methods.
o There can be default implementation of any method specified within an interface.
o Each class that include an interface must implement all of the method

NEED OF INTERFACE:
o To achieve multiple Inheritance.
o We can implement more than one Interface in the one class.
o Methods can be implemented by one or more class.

ACCESSING INTERFACE, VARIABLES & METHODS.


 Once we define the interface we can’t access it directly because we can’t create object of interface.

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 3 of 7


 Define a class which implements your interface, override all the methods of interface in that class with public
access. The class may define its own methods and constructor as well. The variables of interfaces are directly
accessed in all the methods of the class but we cant change its value.
 You can create your own example to show this.

DIFFERENCE BETWEEN CLASS AND INTERFACE

SNo. CLASS INTERFACE

1 It has instance variable. It has final variable.


2 It has non abstract method. It has by default abstract method.
3 We can create object of class. We can‘t create object of interface.
Class has the access specifiers like
4 Interface has only public access specifier
public, private, and protected.
5 Classes are always extended. Interfaces are always implemented.

6 The memory is allocated for the classes. We are not allocating the memory for the interfaces.

EXTENDING AN INTERFACE.
 Interfaces can form hierarchies just like classes.
 Interface can never extends a normal class, but it extends another interface.
 Interface uses extends clause (keywrod) to create a new sub_interface, from the existing super_interface.
 The sub_interface data members are public static final and methods are abstract.
 Important : it is necessary for the class that implements the sub_interface, to override all sub_interface
methods and it’s super_interface method.
 Syntax : interface sub_interface extends super_interface { ……….. }
 Example:
interface Qualification
{
String degree = “Engeneering”; //minimum Qualification
int percentage = 60; // minimum percentage
void showRequirement(); // To print the Requirement
}
interface Trainee extends Qualification
{
int stipend = 3000; // minimum monthly salary
int lectures = 18; // no weekly lecture allotted
void showInfo(); // To print the Working information of Trainee
}

THE IMLPEMENTS CLAUSE.


 A class implementing all of the interfaces using the implements clause (keyword).
 A class can implement more than one interface.

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 4 of 7


 The implements clause consists of the keyword implements followed by a list of interfaces separated by
commas, and it must be put after the extends clause(if there).
 A class that implements an interface, will need to
o Either override all the methods declared in the interface and all its super interfaces.
o or the class must be declared as abstract.
 Any number of dissimilar classes can implement an interface.
 We cant implement a interface in another interface.
 Syntax:
class sub_class [extends super_class] implements interface1 [,interfaceN]
{
//Data members of class;
//Methods of class;
//Overrided methods of all interfaces which we implement in this class
}
Example :
1. class sub_class implements interface { ……… }
2. class sub_class implements interface1,interface2 { ……… }
3. class sub_class extends super_class implements interface { ……… }
4. class sub_class extends super_class implements interface1, interface2 { ……… }
Note:
 Any of these above combinations are permited by java.
 All the public methods of an interface must be overriding in implemented class as public. So If public modifier is
missing, the compiler will issue an error message of weaker access;
 If you forget to override one of the methods in an interface and do not declare your class to be abstract, The
compiler will issue an error message.

THE REFERANCE OF AN INTERFACE.


 We can create a reference variable a class type or an interface type.
 When the referance variable is declared as an interface type, it can reference any object of any class that
implements the interface.
 This is very useful when, interface is implemented by more than one classes.
 Syntax : interface_name ref = new ClassName(); // Here ClassName is the name of class which implement the
interface.
 Example:
interface Exam
{
void evaluation();
}
class Bsc implements Exam
{
public void evaluation()
{

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 5 of 7


System.out.println(“Theory 40 Marks, Practical 50 Marks”);
}
}
class Poly implements Exam
{
public void evaluation()
{
System.out.println(“Theory 100 Marks, Practical 50 Marks”);
}
}
class Test
{
public static void main(String args[])
{
Exam E; // Referance Of Interface
E = new Bsc(); //Referance E contains object of Bsc.
E.evaluation(); //Method of Bsc Class is executed
E = new Poly(); //Referance E contains object of Poly.
E.evaluation(); //Method of Poly Class is executed
}
}

NESTED INTERFACE
 An interface which is declared within another interface or class is known as nested interface.
 The nested interfaces are used to group related interfaces so that they can be easy to maintain.
 The nested interface must be referred by the outer interface or class.
 It can't be accessed directly.
 Some points that should be remembered in nested interface.
o Nested interface must be public if it is declared inside the interface but it can have any access modifier if
declared within the class.
o Nested interfaces are declared static implicitely.
 We can define nested interface in interface or class.
 Syntax Interface within Interface:
Access interface InterfaceName
{
//Members of Outer Interface
public interface nested_InterfaceName
{
//Memebers of Nested Interface
}
}
 Syntax Interface within class:
Access class ClassName
{
//Members of Class
Access interface nested_InterfaceName
{
//Memebers of Nested Interface
}
}

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 6 of 7


 Example :
interface Exam
{
void pattern();
interface Theory { void writtenPaper(); }
interface Practical { void solveAssignment(); }
}

class JavaPaper implements Exam,Exam.Theory,Exam.Practical


{
public void pattern() {
System.out.println("Theory 100 Marks Practical 50 Marks"); }
public void writtenPaper() {
System.out.println("Marks100 Question 6 Passing 40 Marks"); }
public void solveAssignment () {
System.out.println("Solve 2 Assignments on Paper and Computer"); }
public static void main(String args[]) {
Exam.Theory THref=new JavaPaper();//upcasting here
THref.writtenPaper();
Exam.Practical PRref=new JavaPaper();//upcasting here
PRref.solveAssignment();
Exam EXref=new JavaPaper();
EXref.pattern();
}
}

Subject: Java Programming (Interface) Print Date: 10/Apr/2019 Page 7 of 7

You might also like