JavaProgramming_Lecture05
JavaProgramming_Lecture05
00
Object Oriented
Programming
Lecture - 5
Oct, 06th 2022 (4pm-7pm)
Interface: Learning outcomes
- Multilevel inheritance class
- Interface class uses the keyword “implements”
• An interface can extend one or more Java interfaces
classes, which ordinary classes cannot do with
inheritance –
• Sadly Java does not support multiple inheritance
-Abstract VS Interface
-GUI examples
What is interface?
• Shared boundary that can exchange information between 2 or more
separate components of a system
• Exchange can be between computer software , hardware or
peripheral devices , humans or combination of these.
• Why do we need interface in Java? [Answer is in previous slide!]
Interface class implantation provides greater flexibility and excellent
reference type for polymorphism class designs examples
Architects select between interface class implementation and abstract
class implementation
Diamond Problem
}
Arhums-Air:Interface arhumsultana$ java Main.java
The rabbit is fleeing
The hawk is hunting
Cod Fish flees from a bigger fish - Hurra
Whale Fish hunts smaller fish - OOPss!!
Note : Modify your main class by adding new object and constructor
Abstract VS Interface class
An abstract class can extend another Java class and implement multiple Java
interfaces.
Abstract vs Interface
• Abstract class can provide the implementation of the interface.
Interface can’t provide the implementation of an abstract class.
• Abstract class can have final, non-final, static and non-static variables.
The interface has only static and final variables.
• Interface can have only abstract methods. An abstract class can have
both abstract and regular methods.
• Interface class is 100% abstract. All methods are abstract and can be
both public or private
• Interface class rescues when Vehicle and Electric Vehicle class can’t be
inherited by ‘Car’
Writing an abstract class with
interface Class:
Vehicle
Electric
Vehicle
Electric
public interface electric { public Car extends Vehicle { public Jet extends Vehicle {
void runMotor(); void stop(); void propel();
void charge(); void reverse(); void land();
} } }
Extended
interface
Inheritance
GUI – Pressing the button example:
• Clicking a Button (or hitting the "Enter" key on a TextField) fires
an ActionEvent to all its ActionEvent listener(s)
• An ActionEvent listener must implement
the ActionListener interface, which declares
one abstract method called actionPerformed() as follow: