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

Interfaces

Uploaded by

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

Interfaces

Uploaded by

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

SIT Internal

Interfaces
SIT Internal

Learning outcomes
• Motivation for
Interfaces
• Introduction to
interfaces
• Key features
SIT Internal

Abstraction
• Hiding complex, low-level details with a simpler, high-level layer

• The process of hiding certain details and showing only essential


information to the user.
SIT Internal

Let’s Take an Example -


Person
name

- displayDetail()
Person

Teacher Student
Teacher Student
- department - department

- salary() - calculateCGPA()

Teaching Assistant
SIT Internal

Let’s Take an Example -


Person
name

- displayDetail()
Person

Teacher Student
Teacher Student
- department - department

- salary() - calculateCGPA()

Teaching Assistant
SIT Internal

Java does not support multiple inheritance for


classes, meaning a class can only directly inherit
from one superclass

Let’s Take an Example -


Person
name

- displayDetail()
Person

Teacher Student
Teacher Student
- department - department

- salary() - calculateCGPA()

Teaching Assistant
SIT Internal

Let’s Take an Example -


Person
name

- displayDetail()
Person

Teacher Student
Teacher Student
- department - department

- salary() - calculateCGPA()

TeachingAssistant
- department
Teaching Assistant
SIT Internal

TeachingAssistant ta = new TeachingAssistant();


ta.department;

❑ Name clash problem (which department does the TA belong to


Teacher’s or Student’s)
❑Combination problem (if Teacher is from ICT and Student is from
Engineering can the TA have a combined department?)
❑Selection problem (which department do we choose ICT or
Engineering?)
❑Replication problem(should we have two department is the
Teaching Assistant class?)

8
SIT Internal

Interfaces

An interface is a way through which unrelated objects


interact with one another
SIT Internal

Interfaces
• Blueprint for a group of related methods that a class must
implement.

• Defines a contract or a set of method signatures that a class


must adhere to
SIT Internal

Teacher Student
// Teacher interface // Student interface
public interface public interface Student {
Teacher { void study();
void teach(); }
}

public class TeachingAssistant implements Teacher,


Student {
Teaching Assistant
@Override
public void teach() {
System.out.println(name + " (Teaching Assistant)
is assisting in teaching " + subject);
}

@Override
public void study() {
System.out.println(name + " (Teaching Assistant)
is also studying.");
}
SIT Internal

Interfaces: Key Points


• Constant Variables
• Default Abstract methods
• No constructors
• Cannot be instantiated
• Multiple implementation
SIT Internal

Interfaces: Constant Variables


public interface MyInterface {
public static final int CONSTANT_VALUE = 10;
}

• Constant variables are intended to be used as shared values across different


classes that implement the interface
• Non constant variables can be implementing getter and setter methods

public interface MyInterface {


int getNonConstantValue();
void setNonConstantValue(int value);
}
SIT Internal

Interfaces: Abstract Methods public interface MyInterface {


void abstractMethod();
• Abstract methods: Method without any
default void defaultMethod() {
implementation System.out.println("This is a
default method.");
• Default method: provides a default }
}
implementation for the method
SIT Internal

Interfaces: Implementation
• Cannot be instantiated
• A class that implements the interface should implement all the
methods defined in the interface (like a contract)
• Exception: Only if its an abstract class then it does not have to
implement all the methods defined by that interface
SIT Internal

Interfaces: Multiple implementation


public class Person extends Citizen implements Wife, Politician, President {}

public interface Politician {


public interface President
public void joinParty();
{public void winPoll(); }
}

public class Citizen { public interface Wife {


} public void care();}

public class Person { }


SIT Internal

Abstract class Interface

A programmer uses an abstract class A programmer writes an interface when all


Usage when there are some common features the features have different
shared by all the objects. implementations for different objects.

Multiple inheritance not possible (Only Multiple inheritance possible (Multiple


Inheritance one “parent” class) “parent” interfaces)

An abstract class contain both abstract An interface contain only abstract method.
Methods and concrete(non abstract) method

An abstract class can have constructors An interface cannot have constructors


that are used when creating instances of because interfaces cannot be instantiated
Constructor its concrete subclasses. directly.

Abstract class contain any type of variable Interface contain only static final variable
Variables (constant)
SIT Internal

Conclude
• A blueprint for a group of related methods that a class must
implement
• They allow unrelated classes to work together by providing a
common set of methods that they all agree to implement.
• Allows achieving abstraction and polymorphism

You might also like