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

Understanding Java Interfaces Explained

This lesson explains the concept of interfaces in software engineering, highlighting their similarities to abstract classes but emphasizing that interfaces can only contain abstract methods and constants. It discusses the necessity of interfaces due to Java's restriction on multiple inheritance, allowing a class to implement multiple interfaces while extending only one class. The lesson also includes examples of defining and implementing interfaces, as well as converting an abstract class to an interface.

Uploaded by

19mrasul
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 views17 pages

Understanding Java Interfaces Explained

This lesson explains the concept of interfaces in software engineering, highlighting their similarities to abstract classes but emphasizing that interfaces can only contain abstract methods and constants. It discusses the necessity of interfaces due to Java's restriction on multiple inheritance, allowing a class to implement multiple interfaces while extending only one class. The lesson also includes examples of defining and implementing interfaces, as well as converting an abstract class to an interface.

Uploaded by

19mrasul
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

LESSON 8

Interface
Software Engineering

2022 – 2023 a.y.


What is Interface?

● In many ways, Interface is very similar to abstract class but it


marks with interface keyword instead of abstract class
keyword.

public interface InterfaceName {

• Unlike abstract class which can also contain nonabstract


methods, interface contain ONLY abstract methods and
constants.
How to define
interface?
● Defining an interface:
<modifier> interface InterfaceName {
/* constant declarations */
/* method signatures */
}
• For example:
public interface Moveable {
final int MaxMove = 20;
final int MinMove = 1;
public void move();
}
How to use
interface?
● Use implements keyword to implement an interface.
● E.g. Assume we have King implementing Moveable.
public interface Moveable {
final int MaxMove = 20;
final int MinMove = 1;
public String howToMove();
}
public class King implements Moveable {
@Override
public String howToMove() {
return “One step to every direction”;
}
}
Why do we need
interface?
Q. Why don’t we use Abstract class instead of Interface?
A. Java does not allow multiple inheritance! Which means class
can only inherit only one superclass (single inheritance). Like:

public class Sedan extends Vehicle {


}
Why do we need
interface?
Q. Why don’t we use Abstract class instead of Interface?
A. Java does not allow multiple inheritance! Which means class
can only inherit only one superclass (single inheritance). Like:

public class Sedan extends Vehicle {


}

Q. Is there any problem ?


Why do we need
interface?
Q. Why don’t we use Abstract class instead of Interface?
A. Java does not allow multiple inheritance! Which means class
can only inherit only one superclass (single inheritance). Like:

public class Sedan extends Vehicle {


}

Q. Is there any problem ?


A. No
Why do we need
interface?
Q. Why don’t we use Abstract class instead of Interface?
A. Java does not allow multiple inheritance! Which means class
can only inherit only one superclass (single inheritance). Like:

public class Sedan extends Vehicle {


}

Q. Is there any problem ?


A. Not yet.
Why do we need
interface?
Q. Why if we want to use JApplet in Sedan as well? Can we do
like this?

public class Sedan extends Vehicle, JApplet {


}
Why do we need
interface?
Q. Why if we want to use JApplet in Sedan as well? Can we do
like this?

public class Sedan extends Vehicle, JApplet {


}

A. No we can not. We will get compile error.

• So, we have to make one of these classes (Vehicle and JApplet) to be


interface. Because we can inherit one class and implement the other
class.
Why do we need
interface?
Q. Why if we want to use JApplet in Sedan as well? Can we do
like this?

public class Sedan extends Vehicle, JApplet {


}

A. No we can not. We will get compile error.

• So, we have to make one of these classes (Vehicle and JApplet) to be


interface. Because we can inherit one class and implement the other
class.
• But which one should we choose to be interface?
Why do we need
interface?
Q. Why if we want to use JApplet in Sedan as well? Can we do
like this?

public class Sedan extends Vehicle, JApplet {


}

A. No we can not. We will get compile error.

• So, we have to make one of these classes (Vehicle and JApplet) to be


interface. Because we can inherit one class and implement the other
class.
• But which one should we choose to be interface?
• JApplet is a Java API. You cannot change it. So, you can only change
your Vehicle from abstract class to interface.
Abstract ->
Interface
public abstract class Vehicle {
public abstract String getTradeMark();
public abstract double getSpeed();
public abstract boolean hasWeapon();
}
• And here is the change to interface:
public interface Vehicle {
public String getTradeMark();
public double getSpeed();
public boolean hasWeapon();
}
• Methods are always abstract automatically, you do not need to mark
them.
Why do we need
interface?
Changes in the Sedan class:

public class Sedan implements Vehicle{


public String getTradeMark() {
return “Jeep”;
}
public double getSpeed() {
return 220;
}
public boolean hasWeapon() {
return false;
}
}
Multiple Interface

• Java allow to have MULTIPLE INTERFACE. That is, you can inherit
only one class, but you can have many interfaces. E.g.

public class Sedan extends JApplet implements Vehicle, Comparable, Car {


/* Override methods are here */
}
Interface

• In interface, you cannot include nonabstract methods at all.


• In abstract class, you can mix nonabstract (regular) methods and abstract
methods together.
THANK
YOU!
ANY QUESTIONS?

[Link]@[Link]

Abdurasul Bobonazarov

You might also like