Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

JavaProgramming_Lecture05

Uploaded by

stacy russo
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)
14 views

JavaProgramming_Lecture05

Uploaded by

stacy russo
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
You are on page 1/ 20

AP/ITEC 2610 3.

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

• Java faces ambiguity between two parent classes!


• Super Class and Class A , B – which is Multiple inheritance
• This particular problem is called the Diamond problem
• Super class is being inherited by Class A and Class B
• We want to inherit the properties of Class A and Class B into a new
class - Class C
• Impossible! Seriously ?
Let’s solve it by creating 2 subclasses:

public class Main { public interface Prey {


public static void main(String[] args) { void flee();
Rabbit r = new Rabbit(); }
Hawk h = new Hawk();
r.flee(); public interface Predator {
h.hunt(); void hunt();
} }
}

public class Rabbit implements Prey{


public void flee() {

System.out.println("The rabbit is fleeing"); } public class Hawk implements Predator{


} public void hunt() {

System.out.println("The hawk is hunting"); }


}
Interfacing with both Prey Class and Predator Class:
Multi level Inheritance: combined using interface class
public class Fish implements Prey , Predator{

public void hunt() {

System.out.println("Whale Fish hunts smaller fish - OOPss!!"); }

public void flee() {


System.out.println("Cod Fish flees from a bigger fish - Hurra"); }

}
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

Car Jet Truck

public interface electric { public Car extends Vehicle {


void runMotor(); void stop();
void charge(); void reverse();
} }
Abstract class
ApplianceInterface in java provide a way to achieve abstra

Vehicle
Electric

Heater Car Jet Truck

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

interfaces should provide implementation for all


the methods declared in the interface.
Example of Interface Class: GUI

/* Hello GUI Class */

import javax.swing.*; // JFrame, JMenuBar, JMenu, JMenuItem


import java.awt.event.*; // ActionListener, ActionEvent

public class HelloGUI { … }

class ImagePanel extends JPanel implements MouseListener {


/* Get Full Code from Eclass */

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:

public interface ActionListener {


public void actionPerformed(ActionEvent evt);
// Called back upon button-click (on Button), enter-key pressed (on TextField)
}
GUI – Mouse Event example:
MouseEvent and MouseMotionListener Interface

• To handle the mouse-move and mouse-dragm


the MouseMotionListener interface declares the following two
abstract methods:
public void mouseDragged(MouseEvent e)
// Called-back when a mouse-button is pressed on the source
component and then dragged.
public void mouseMoved(MouseEvent e)
// Called-back when the mouse-pointer has been moved onto the
source component but no buttons have been pushed.
Method @Override in Java:
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
• In other words, If a subclass provides the specific implementation of
the method that has been declared by one of its parent class, it is
known as method overriding.
Usage of Java Method Overriding
• Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
• Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
Examples:
Method Overriding *No interface*
Homework:
• Expand the hello world GUI application by using both mouse and
keyboard interface classes (with multiple inheritance).
• Write down the interface class for slide # 14’s two methods.
Next Class, Next Assignment &
Midterm Test:
• Recursion in Java
• Assignment 1B (9 marks)
• Topics: Classes , Inheritance and GUI [Lecture 2, 3 and 4]
E-class Submission: Friday 14th Oct 2022 - 11:59pm (sharp)
• Mid Term Test:
Oct 27th 2022 @4:05pm [120mins]
0005 Accolade East Building
Topics: All topics till 20th Oct 2022– which is 1 week after!
Good luck!
Online reading
• Java Abstract class/method
https://www.w3schools.com/java/java_abstract.asp

• Java Interface (w3schools.com)


https://www.w3schools.com/java/java_abstract.asp

• Java Interface for GUI apps


https://www3.ntu.edu.sg/home/ehchua/programming/java/j4a_gui.html

HAPPY READING WEEK!

You might also like