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

Lecture 4 - Interfaces

Uploaded by

Sahan Mandira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 4 - Interfaces

Uploaded by

Sahan Mandira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CS2832: Modular Software Development

OOP Concepts - Interface

Department of Computer Science and Engineering


University of Moratuwa
Overview

 Abstract Classes

 Interfaces

 Inner Classes
Abstract Classes
 An abstract class is a class that is declared abstract
 Abstract classes can not be instantiated, but can be sub - classed
 Abstract classes are used only as super classes in inheritance hierarchies
 Abstract class are used to share a common design
Abstract Classes
public abstract class Animal {
//declare fields
//declare non - abstract methods
public abstract void makeNoise();

}
 When an abstract class is sub-classed, all of the abstract methods in its parent class
must be implemented (concrete class)
 However, if it does not, then the subclass must also be declared abstract
Abstract Methods
 An abstract method is a method that is declared without an
implementation
abstract void moveTo( double x, double y);
Abstract method is followed by a semicolon but no braces
Is there something more abstract?

Class Abstract Class Interface


Well defined Partially defined 100% abstract
0% abstract 0% to 100% abstract
Interfaces
 An Interface is a reference type

public interface Bicycle {


void changeCadence (int newValue);
void changeGear (int newValue );
void speedUp(int increment);
void applyBrakes (int decrement);
}
The Interface Body
 Interface can contain only
 Constants (static and final variable)
 Abstract methods
 Static methods
The Interface Body
public interface TimeClient {
static final String DEFAULT_TIME_FORMAT = “HH -MM -YYYY: HH.MM”; Constant

void setTime (int hour, int minute, int second); Abstract method
void setDate (int day, int month, int year);

static ZoneId getZoneId (String zoneString) {


try {
return ZoneId.of (zoneString);
} catch ( DateTimeException e) {
Static method
System.err.println ("Invalid time zone: " + zoneString + "; using default time zone instead.");
return ZoneId.systemDefault ();
}
}

}
The Interface Body
 All abstract and static methods are implicitly public
 All constant values are implicitly public, static and final
Implementing an Interface
 implements keyword is used to declare a class that implements in
interface
 A class can implement more than one interface
Interfaces implemented by the class are specified after implements keyword as
a comma - separated list

 By convention, the implements clause follows the extends clause


A class that implements an interface must implement all the abstract
methods declared in the interface
Implementing an Interface
public interface Movable {
void move();
}

public interface Rotatable {


void rotate();
}

public class Shape implements Movable, Rotatable {


//implement abstract methods
public void move() {
//method body
}

public void rotate() {


//method body
}
}
Extending an Interface

 An interface can extend another interface only


 Allow adding new methods to the interface

public interface IVehicle {


void moveForward ();
void moveBackward();
}
public interface IAirplane extends IVehicle {
void moveUp ();
void moveDown ();
}
Multiple Inheritance

Flying Horse
Multiple Inheritance
public class Horse { pubic class Eagle {
int noOfLegs = 4; int noOfLegs = 2;
public void makeNoise () { public void makeNoise () {
System.out.printlin (“Neigh”); System.out.pintln (“Eagle Sound”);
} }
} public void fly() {}
}
public class Hippogriff extends Horse, Eagle{
int noOfLegs - > 2 or 4 ???
public void makeNoise() {} - > “Neigh” or “Eagle Sound ” ???
}
Multiple Inheritance by Type
public class Horse { Public interface Flyable {
int noOfLegs = 4; public void fly();
public void makeNoise () { }
System.out.printlin (“Neigh”);
}
}
public class Hippogriff extends Horse implements Flyable {
public void fly() {
System.out.println (“Hippogriff Flying”);
}
}
Abstract Class vs. Interface

Abstract Class Interface

 Cannot be instantiated  Cannot be instantiated


 Can declare non-static, non-final fields  All fields are automatically public, static and
final
 Can define public, protected, private  All methods are public (abstract, static)
concrete methods
 Single inheritance  Multiple inheritance by type

 Can share common fields and methods  Can share interface among several unrelated
among several closely related classes classes
Inner Class
 A class within a class or method.
 Normally a class cannot be private. However, you can make inner classes private.
-> Can be used as a security mechanism
public class OuterClass { public class MainClass {
private class InnerClass { public static void main(String args[]) {
public void someFuction() {…….} class AntotherInnerClass {…….}
AnotherInnerClass aIC = new AnotherInnerClass();
}

public void accessInner() { OuterClass outerCls = new OuterClass();


InnerClass innerCls = new InnerClass(); outerCls.accessInner();

innerCls.someFunction(); }

} }

}
A real-world example of Inner Class
If you have Questions/Suggestions,
please bring them up in the forums!
Thank You

You might also like