KEMBAR78
Module3opp Part2 | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
31 views15 pages

Module3opp Part2

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)
31 views15 pages

Module3opp Part2

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/ 15

Interfaces

Using the keyword interface, we can fully abstract a class’ interface from its implementation.
That is, using interface, we can specify what a class must do, but not how it does it.
Interface are syntactically similar to classes, but they lack instance variables, and their methods are declared
without any body.

Once it is defined, any number of classes can implement an interface.


Note:
Using interfaces, we can perform multiple inheritance.
Using Inheritance, we can fully abstract a class.
Methods within interfaces are declared without any body.
Any number of classes can implement an interface.
One class can implement any number of interfaces.
“one interface, multiple methods” So it points to polymorphism feature of java.
Example: (NOT Possible Only for explanation)
class Sample
Interfaces {
void show()
The mechanism of inheriting the features of more than one {
base class into a single class is known as multiple System.out.println("Sample show");
inheritance. }
}
class Test
{
void show()
The problem occurs when there exist methods with same {
signature in both the super classes. System.out.println("Test Show");
On calling the method, the compiler cannot determine }
which class method to be called and even on calling which }
class method gets the priority. class Demo extends Sample,Test //Not Possible
Java does not support multiple inheritance but the {
multiple inheritance can be achieved by using the
interface. Multiple inheritance is not supported because of }
ambiguity. But it is supported in case of interface because class Main {
there is no ambiguity as implementation is provided by the public static void main(String[] args) {
implementation class. Demo d=new Demo();
d.show();
}
}
Interfaces - Defining an Interface
• An interface in java is a blueprint of a class.
• Interface fields are public, static and final by default, and methods are public and abstract.
• It cannot be instantiated just like abstract class.
• The main reasons to use interface are:
• To achieve abstraction.
• To support the functionality of multiple inheritance.
• The methods that are declared have no bodies. They end with a semicolon after the parameter list.
• They are abstract methods; there can be no default implementation of any method specified within an interface.
• Variables can be declared inside the interface. They are implicitly final and static, meaning they cannot be
changed by the implementing class.
• All methods and variables are implicitly public.
Interfaces - Defining an Interface
An interface is defined much like a class. This is the general form of an interface:

interface Sample
{
int a=10;
void method1();
}

When no access specifier is included, then default access results, and the interface is only available to other members
of the package in which it is declared.
When it is declared as public, the interface can be used by any other code.
Interfaces - Defining an Interface
Interfaces - Defining an Interface
Interfaces - Implementing Interfaces
• Once interface is defined, any number of classes can implement an interface, using implements keyword.
• Also, one class can implement any number of interfaces.
• To implement an interface, a class must create the complete set of methods defined by the interface.
• Difference between abstract class and interface- Abstract class can have both abstract and concrete methods but
interface has only abstract methods and thereby achieves 100%abstraction.
Syntax : class classname [extends superclass] [implements interface [,interface...]]
{ class B extends A implements Inter1,Inter2
// class-body {
}
}
• If a class implements more than one interface, then the interfaces are separated with a comma.
• The methods that implement an interface must be declared public.
• The type signature of the implementing method must match exactly the type signature specified in the interface
definition.
• Variables in Interfaces - Declare variables in an interface that are initialized to the desired values and the same can
be used in the implemented class.
Interfaces - Implementing interface Inter1
{
Interfaces -Example int a=10;
void method1();
}
• It is both permissible and common for classes that
class Test implements Inter1
implement interfaces to define additional members
{
of their own.
public void method1()
{
System.out.println("Test Interface Method");
System.out.println(a);
}
void method2()
{
System.out.println("Test Non Interface Method");
}
OUTPUT }
Test Interface Method class Demo
10 {
public static void main(String a[]) {
Test t=new Test();
t.method1();
}
}
Interfaces - Variables in Interfaces
• We can declare variables in an interface that are initialized to the desired values and the same can be used in the
implemented class.
• We can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains
variables that are initialized to the desired values.
• When we include that interface in a class (that is, when we “implement” the interface), all of those variable names
will be in scope as constants.
• If an interface contains no methods, then any class that includes such an interface doesn’t actually implement
anything.
• It is as if that class were importing the constant fields into the class name space as final variables.

interface SharedConstants {
int NO = 0;
int YES = 1;
int MAYBE = 2;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
}
interface Inter1
Interfaces – Multiple {
void method1();
Inheritence -Example }
interface Inter2
• A class can implements multiple interfaces {
known as multiple inheritance. void method1();
• If a class implements two interfaces that declare }
the same method, then the same method will be class Test implements Inter1,Inter2
used by clients of either interface. {
public void method1()
{
System.out.println("Test Method1 Multiple Inheritance");
}
}
class Demo
{
public static void main(String a[])
{
Output: Test t=new Test();
t.method1();
Test Method1 Multiple Inheritance }
}
interface Inter1
Interfaces - Accessing Implementations {
Through Interface References void method1();
}

• We can declare object references to an class Test implements Inter1


interface. {
• These references can be used to point to the public void method1()
instance of any class that implements the {
declared interface. System.out.println("Test Method");
}
• When we call a method through one of these }
references, the correct version will be called class Demo
based on the actual instance of the interface {
being referred. public static void main(String a[])
• The class which implements the interface can {
have its own methods also, but the reference Inter1 t=new Test();
variable cannot access that method. t.method1();
}
Output: }
Test Method
interface Inter1
Interfaces – Partial {
Implementations void method1();
void method2();
}
• If a class includes an interface but does abstract class Test3 implements Inter1 {
not fully implement the methods public void method1() {
defined by that interface, then that class System.out.println("Test Method1");
}
must be declared as abstract. }
Output: Test Method1 class Demo3 extends Test3
{
Demo Method2 public void method2()
{
System.out.println("Demo Method2");
}
}
class Demo4
{
public static void main(String a[]) {
Demo3 t=new Demo3();
t.method1();
t.method2();
}
}
Interfaces – Interfaces interface
{
Intr1 class Demo4
{
Can Extend another void meth1();
void meth2();
public static void main(String arg[])
{
interface } MyClass ob = new MyClass();
• One interface can inherit another by use
interface Intr2 extends Intr1 ob.meth1();
of the keyword extends. { ob.meth2();
• Any class that implements an interface void meth3(); ob.meth3();
must implement all methods defined by } }
that interface, including the methods class MyClass implements Intr2 }
from the inherited interfaces. {
public void meth1()
{
System.out.println("Implement meth1().");
}
public void meth2()
{
System.out.println("Implement meth2().");
Output: Implement meth1(). }
Implement meth2(). public void meth3()
{
Implement meth3().
System.out.println("Implement meth3().");
}
}
class Demo5 extends MyClass1 implements Inte1,Inte2
Interfaces – Class Extends and Implements {
interface Inte1 public void meth1()
{ {
void meth1(); System.out.println("Implement meth1().");
} }
interface Inte2 public void meth2()
{ {
A class can extend only void meth2(); System.out.println("Implement meth2().");
one class and can } }
implements any number interface Inte3 }
of interfaces. { class Demo6
void meth3(); {
Output: } public static void main(String arg[])
class MyClass1 implements Inte3 {
Implement meth1().
{ Demo5 ob = new Demo5();
Implement meth2(). public void meth3() ob.meth1();
Implement meth3(). { ob.meth2();
System.out.println("Implement meth3()."); ob.meth3();
} }
} }
Interfaces – Nested Interface interface Interr1
{
interface Interr2
{
void show();
An interface can be declared as a member of a class or another interface.
}
Such an interface is called a member interface or a nested interface. }
Syntax: interface interface_name class Testing implements Interr1.Interr2
{ {
//method and fields public void show()
interface nested_interface_name {
{ System.out.println("show method of interface");
//methods and fields }
}
}
}
class A4
class class_name {
{ public static void main(String[] args)
//methods and fields {
interface nested_interface_name Testing t = new Testing();
{ t.show();
//methods and fields }
}
}
}

You might also like