0% found this document useful (0 votes)
15 views

Lecture 15 OOP

Uploaded by

Ashikur Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 15 OOP

Uploaded by

Ashikur Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ICE 127: Object Oriented Programming

Credit Hour: 3
Lec - 15

Course Teacher
Md Istakiak Adnan Palash

Department of Information & Communication Engineering


Daffodil International University

Lec - 15 1
Java interface
• A Java interface is a bit like a class, except a Java interface can
only contain method signatures and fields. An Java
interface cannot contain an implementation of the methods,
only the signature (name, parameters and exceptions) of the
method. You can use interfaces in Java as a way to achieve
polymorphism.
• Since Java 8, we can have method body in interface. But we
need to make it default method.
• Since Java 8, we can have static method in interface.
• Java Interface also represents IS-A relationship.
• It cannot be instantiated just like abstract class.
Lec - 15 2
Why use Java interface?

• There are mainly three reasons to use interface. They are given below.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple inheritance.

Lec - 15 3
Internal addition by compiler in interface

• Interface fields are public, static and final by default, and methods
are public and abstract.

Lec - 15 4
Understanding relationship between classes and
interfaces
• As shown in the figure given below, a class extends another class, an
interface extends another interface but a class implements an interface.

Lec - 15 5
Java Interface Example 1
interface printable{
void print();
Output:
}
Hello
class A6 implements printable{
public void print(){System.out.println("Hello");}
In this example,
Printable interface has
only one method, its
public static void main(String args[]){
implementation is
A6 obj = new A6();
provided in the A class.
obj.print();
}
}

Lec - 15 6
Java Interface Example 2
//Interface declaration: by first user Output:
interface Drawable{ drawing circle
void draw();
} Here, Drawable interface has
//Implementation: by second user only one method. Its
class Rectangle implements Drawable{ implementation is provided by
public void draw(){System.out.println("drawing rectangle");} Rectangle and Circle classes.
}
class Circle implements Drawable{ In real scenario, interface is
public void draw(){System.out.println("drawing circle");} defined by someone but
} implementation is provided by
//Using interface: by third user different implementation
class TestInterface1{ providers.
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by meth And, it is used by someone
od e.g. getDrawable() else. The implementation part is
d.draw(); hidden by the user which uses
}} the interface.

Lec - 15 7
Multiple inheritance in Java by interface

• Multiple inheritance is not supported in case of class because of


ambiguity. But it is supported in case of interface.
• If a class implements multiple interfaces, or an interface extends
multiple interfaces i.e. known as multiple inheritance.
Lec - 15 8
Multiple inheritance in Java by interface
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");} Output: Hello Welcome
public void show(){System.out.println("Welcome");}

public static void main(String args[]){


A7 obj = new A7();
obj.print();
obj.show();
}
}
Lec - 15 9
Multiple inheritance in Java by interface
• Question: Multiple inheritance is not supported through class in java but
it is possible by interface, why?
• Answer: As we have explained in the inheritance chapter, multiple
inheritance is not supported in case of class because of ambiguity. But it
is supported in case of interface because there is no ambiguity as
implementation is provided by the implementation class. Example : Next
slide.

Lec - 15 10
Multiple inheritance in Java by interface
interface Printable{
void print();
}
Output:
interface Showable{
void print(); Hello
}
As you can see in the above
class TestInterface3 implements Printable, Showable{ example, Printable and
Showable interface have
public void print(){System.out.println("Hello");} same methods but its
public static void main(String args[]){ implementation is provided by
TestInterface3 obj = new TestInterface3(); class TestInterface3, so there
obj.print(); is no ambiguity.
}
}
Lec - 15 11
Interface Inheritance
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
} Output:
class TestInterface4 implements Showable{ Hello
public void print(){System.out.println("Hello");} Welcome
public void show(){System.out.println("Welcome");}
public static void main(String args[]){ A class implements interface
TestInterface4 obj = new TestInterface4();
but one interface extends
obj.print();
obj.show();
another interface .
}
}

Lec - 15 12
Java 8 Default Method in Interface
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
} Output:
class Rectangle implements Drawable{ drawing rectangle
public void draw(){System.out.println("drawing rectangle") default method
;}
} Since Java 8, we can have
class TestInterfaceDefault{ method body in interface.
public static void main(String args[]){ But we need to make it
Drawable d=new Rectangle(); default method.
d.draw();
d.msg();
}}
Lec - 15 13
Java 8 Static Method in Interface
interface Drawable{
void draw();
static int cube(int y){return y*y*y;}
}
class Rectangle implements Drawable{ Output:
public void draw(){System.out.println("drawing rectangle");}
} drawing rectangle
27
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Lec - 15 14
Difference between abstract class and interface
Abstract class and interface both are used to achieve abstraction where we can declare the abstract
methods. Abstract class and interface both can't be instantiated.
But there are many differences between abstract class and interface.
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since Java 8, it
abstract methods. can have default and static methods also.

2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and non- Interface has only static and final variables.
static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of abstract
interface. class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Lec - 15 15
//Creating interface that has 4 methods
interface A{
void a();//bydefault, public and abstract
void b();
void c();
void d(); }
//Creating abstract class that provides the implementation of one method of A interface
abstract class B implements A{ Example of
public void c(){System.out.println("I am C");} abstract class
}
//Creating subclass of abstract class, now we need to provide the implementation of rest of the methods
and interface
class M extends B{ in Java
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");} Output:
} I am a
//Creating a test class that calls the methods of A interface
class Test5{ I am b
public static void main(String args[]){ I am c
A a=new M(); I am d
a.a();
a.b();
a.c();
a.d();
}}
Lec - 15 16
Nested Interface in Java

interface printable{
void print();
interface MessagePrintable{
void msg();
}
}

Lec - 15 17
Thank You

Lec - 15 18

You might also like