SlideShare a Scribd company logo
Java Programming –
Abstract Class & Interface
Oum Saokosal
Master’s Degree in information systems,Jeonju
University,South Korea
012 252 752 / 070 252 752
oumsaokosal@gmail.com
Contact Me
• Tel: 012 252 752 / 070 252 752
• Email: oumsaokosal@gmail.com
• FB Page: https://facebook.com/kosalgeek
• PPT: http://www.slideshare.net/oumsaokosal
• YouTube: https://www.youtube.com/user/oumsaokosal
• Twitter: https://twitter.com/okosal
• Web: http://kosalgeek.com
Abstract Classes and Interfaces
The objectives of this chapter are:
To explore the concept of abstract classes
To understand interfaces
To understand the importance of both.
What is an Abstract class?
Superclasses are created through the process called
"generalization"
Common features (methods or variables) are factored out of
object classifications (ie. classes).
Those features are formalized in a class. This becomes the
superclass
The classes from which the common features were taken
become subclasses to the newly created super class
Often, the superclass does not have a "meaning" or
does not directly relate to a "thing" in the real world
It is an artifact of the generalization process
Because of this, abstract classes cannot be instantiated
They act as place holders for abstraction
Vehicle
- make: String
- model: String
- tireCount: int
Car
- trunkCapacity:
int
Abstract superclass:
Abstract Class Example
In the following example, the subclasses represent objects
taken from the problem domain.
The superclass represents an abstract concept that does not
exist "as is" in the real world.
Truck
- bedCapacity: int
Note: UML represents abstract
classes by displaying their name
in italics.
What Are Abstract Classes Used For?
Abstract classes are used heavily in Design Patterns
Creational Patterns: Abstract class provides interface for creating
objects. The subclasses do the actual object creation
Structural Patterns: How objects are structured is handled by an
abstract class. What the objects do is handled by the subclasses
Behavioural Patterns: Behavioural interface is declared in an abstract
superclass. Implementation of the interface is provided by subclasses.
Be careful not to over use abstract classes
Every abstract class increases the complexity of your
design
Every subclass increases the complexity of your design
Ensure that you receive acceptable return in terms of functionality given
the added complexity.
Defining Abstract Classes
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object
public abstract class Vehicle
{
private String make;
private String model;
private int tireCount;
[...]
public class Car extends Vehicle
{
private int trunkCapacity;
[...]
Vehicle
- make: String
- model: String
- tireCount: int
Car
- trunkCapacity: int
Truck
- bedCapacity: int
public class Truck extends Vehicle
{
private int bedCapacity;
[...] Often referred to as
"concrete" classes
Abstract Methods
Methods can also be abstracted
An abstract method is one to which a signature has been provided, but
no implementation for that method is given.
An Abstract method is a placeholder. It means that we declare that a
method must exist, but there is no meaningful implementation for that
methods within this class
Any class which contains an abstract method MUST also be
abstract
Any class which has an incomplete method definition
cannot be instantiated (ie. it is abstract)
Abstract classes can contain both concrete and abstract
methods.
If a method can be implemented within an abstract class, and
implementation should be provided.
Abstract Method Example
In the following example, a Transaction's value can be
computed, but there is no meaningful implementation that
can be defined within the Transaction class.
How a transaction is computed is dependent on the transaction's type
Note: This is polymorphism.
Transaction
- computeValue(): int
RetailSale
- computeValue(): int
StockTrade
- computeValue(): int
Defining Abstract Methods
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object
public abstract class Transaction
{
public abstract int computeValue();
public class RetailSale extends Transaction
{
public int computeValue()
{
[...]
Transaction
- computeValue(): int
RetailSale
- computeValue(): int
StockTrade
- computeValue(): int
public class StockTrade extends Transaction
{
public int computeValue()
{
[...]
Note: no implementation
What is an Interface?
An interface is similar to an abstract class with the following
exceptions:
All methods defined in an interface are abstract. Interfaces can contain
no implementation
Interfaces cannot contain instance variables. However, they can
contain public static final variables (ie. constant class variables)
• Interfaces are declared using the "interface" keyword
If an interface is public, it must be contained in a file which
has the same name.
• Interfaces are more abstract than abstract classes
• Interfaces are implemented by classes using the
"implements" keyword.
Declaring an Interface
public interface Steerable
{
public void turnLeft(int degrees);
public void turnRight(int degrees);
}
In Steerable.java:
public class Car extends Vehicle implements Steerable
{
public int turnLeft(int degrees)
{
[...]
}
public int turnRight(int degrees)
{
[...]
}
In Car.java:
When a class "implements"an
interface, the compiler ensures that
it provides an implementationfor
all methods definedwithin the
interface.
Implementing Interfaces
A Class can only inherit from one superclass. However, a
class may implement several Interfaces
The interfaces that a class implements are separated by commas
• Any class which implements an interface must provide an
implementation for all methods defined within the interface.
NOTE: if an abstract class implements an interface, it NEED NOT
implement all methods defined in the interface. HOWEVER, each
concrete subclass MUST implement the methods defined in the
interface.
• Interfaces can inherit method signatures from other
interfaces.
Declaring an Interface
public class Car extends Vehicle implements Steerable, Driveable
{
public int turnLeft(int degrees)
{
[...]
}
public int turnRight(int degrees)
{
[...]
}
// implement methods defined within the Driveable interface
In Car.java:
Inheriting Interfaces
If a superclass implements an interface, it's subclasses also
implement the interface
public abstract class Vehicle implements Steerable
{
private String make;
[...]
public class Car extends Vehicle
{
private int trunkCapacity;
[...]
Vehicle
- make: String
- model: String
- tireCount: int
Car
- trunkCapacity: int
Truck
- bedCapacity: int
public class Truck extends Vehicle
{
private int bedCapacity;
[...]
Multiple Inheritance?
Some people (and textbooks) have said that allowing classes
to implement multiple interfaces is the same thing as multiple
inheritance
This is NOT true. When you implement an interface:
The implementing class does not inherit instance variables
The implementing class does not inherit methods (none are defined)
The Implementing class does not inherit associations
Implementation of interfaces is not inheritance. An interface
defines a list of methods which must be implemented.
Interfaces as Types
When a class is defined, the compiler views the class as a
new type.
The same thing is true of interfaces. The compiler regards an
interface as a type.
It can be used to declare variables or method parameters
int i;
Car myFleet[];
Steerable anotherFleet[];
[...]
myFleet[i].start();
anotherFleet[i].turnLeft(100);
anotherFleet[i+1].turnRight(45);
Abstract Classes Versus Interfaces
When should one use an Abstract class instead of an
interface?
If the subclass-superclass relationship is genuinely an "is a"
relationship.
If the abstract class can provide an implementation at the appropriate
level of abstraction
• When should one use an interface in place of an Abstract
Class?
When the methods defined represent a small portion of a class
When the subclass needs to inherit from another class
When you cannot reasonably implement any of the methods

More Related Content

PPT
Java interfaces
Raja Sekhar
 
PPTX
Java- Nested Classes
Prabhdeep Singh
 
PPTX
Java input
Jin Castor
 
PPT
L11 array list
teach4uin
 
PPT
Collection Framework in java
CPD INDIA
 
PPTX
Control Statements in Java
Niloy Saha
 
PDF
Polymorphism In Java
Spotle.ai
 
PPTX
Constructor in java
Hitesh Kumar
 
Java interfaces
Raja Sekhar
 
Java- Nested Classes
Prabhdeep Singh
 
Java input
Jin Castor
 
L11 array list
teach4uin
 
Collection Framework in java
CPD INDIA
 
Control Statements in Java
Niloy Saha
 
Polymorphism In Java
Spotle.ai
 
Constructor in java
Hitesh Kumar
 

What's hot (20)

PPT
Inheritance in java
Lovely Professional University
 
PPT
Functions in c++
Maaz Hasan
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
ArrayList in JAVA
SAGARDAVE29
 
PPTX
Java - Collections framework
Riccardo Cardin
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PDF
Constructor and Destructor
Kamal Acharya
 
PPTX
Friend functions
Megha Singh
 
PPS
String and string buffer
kamal kotecha
 
PPTX
collection framework in java
MANOJ KUMAR
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPT
Python Dictionaries and Sets
Nicole Ryan
 
PPTX
Inheritance In Java
Manish Sahu
 
PDF
Object oriented programming With C#
Youssef Mohammed Abohaty
 
PPTX
Java program structure
Mukund Kumar Bharti
 
PPTX
Ppt on this and super keyword
tanu_jaswal
 
Inheritance in java
Lovely Professional University
 
Functions in c++
Maaz Hasan
 
Inheritance In Java
Darpan Chelani
 
Type casting in java
Farooq Baloch
 
This keyword in java
Hitesh Kumar
 
ArrayList in JAVA
SAGARDAVE29
 
Java - Collections framework
Riccardo Cardin
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Constructor and Destructor
Kamal Acharya
 
Friend functions
Megha Singh
 
String and string buffer
kamal kotecha
 
collection framework in java
MANOJ KUMAR
 
Method overloading
Lovely Professional University
 
classes and objects in C++
HalaiHansaika
 
Python Dictionaries and Sets
Nicole Ryan
 
Inheritance In Java
Manish Sahu
 
Object oriented programming With C#
Youssef Mohammed Abohaty
 
Java program structure
Mukund Kumar Bharti
 
Ppt on this and super keyword
tanu_jaswal
 
Ad

Viewers also liked (20)

PDF
Java OOP Programming language (Part 7) - Swing
OUM SAOKOSAL
 
PDF
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
PPTX
Android app development - Java Programming for Android
OUM SAOKOSAL
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
KEY
Practical OOP In Java
wiradikusuma
 
PDF
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
OUM SAOKOSAL
 
PDF
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
PPT
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
PPT
Basic concepts of object oriented programming
Sachin Sharma
 
PPT
Java OOP s concepts and buzzwords
Raja Sekhar
 
PPT
Java database connectivity
Vaishali Modi
 
PPTX
Object+oriented+programming+in+java
Ye Win
 
PDF
Copy As Interface | Erika Hall | Web 2.0 Expo
Erika Hall
 
PPTX
Java interface
BHUVIJAYAVELU
 
PPT
Measuring And Defining The Experience Of Immersion In Games
OUM SAOKOSAL
 
PPT
Chapter 7 String
OUM SAOKOSAL
 
PPT
ITS (Intelligent Teleportation System)
OUM SAOKOSAL
 
PPT
Terminology In Telecommunication
OUM SAOKOSAL
 
PPT
Chapter 9 Interface
OUM SAOKOSAL
 
Java OOP Programming language (Part 7) - Swing
OUM SAOKOSAL
 
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Android app development - Java Programming for Android
OUM SAOKOSAL
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Practical OOP In Java
wiradikusuma
 
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
OUM SAOKOSAL
 
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
Basic concepts of object oriented programming
Sachin Sharma
 
Java OOP s concepts and buzzwords
Raja Sekhar
 
Java database connectivity
Vaishali Modi
 
Object+oriented+programming+in+java
Ye Win
 
Copy As Interface | Erika Hall | Web 2.0 Expo
Erika Hall
 
Java interface
BHUVIJAYAVELU
 
Measuring And Defining The Experience Of Immersion In Games
OUM SAOKOSAL
 
Chapter 7 String
OUM SAOKOSAL
 
ITS (Intelligent Teleportation System)
OUM SAOKOSAL
 
Terminology In Telecommunication
OUM SAOKOSAL
 
Chapter 9 Interface
OUM SAOKOSAL
 
Ad

Similar to Java OOP Programming language (Part 6) - Abstract Class & Interface (20)

PPT
Abstract class
Fraboni Ec
 
PPT
Abstract class
James Wong
 
PPT
Abstract class
Hoang Nguyen
 
PPT
Abstract class
Harry Potter
 
PPT
Abstract class
Young Alista
 
PPT
Abstract class
Tony Nguyen
 
PPT
Abstract class
Luis Goldster
 
PPT
Java interfaces & abstract classes
Shreyans Pathak
 
PPTX
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
PPTX
Structural pattern 3
Naga Muruga
 
PPTX
Java OOPS Concept
Richa Gupta
 
PPT
Object Oriented Programming with Java
backdoor
 
PPT
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
PPT
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
PPTX
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
patilrohini0224
 
PPT
Design Patterns
Rafael Coutinho
 
PPTX
Framework Design Guidelines For Brussels Users Group
brada
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PPT
P Training Presentation
Gaurav Tyagi
 
Abstract class
Fraboni Ec
 
Abstract class
James Wong
 
Abstract class
Hoang Nguyen
 
Abstract class
Harry Potter
 
Abstract class
Young Alista
 
Abstract class
Tony Nguyen
 
Abstract class
Luis Goldster
 
Java interfaces & abstract classes
Shreyans Pathak
 
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Structural pattern 3
Naga Muruga
 
Java OOPS Concept
Richa Gupta
 
Object Oriented Programming with Java
backdoor
 
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
patilrohini0224
 
Design Patterns
Rafael Coutinho
 
Framework Design Guidelines For Brussels Users Group
brada
 
oops with java modules i & ii.ppt
rani marri
 
P Training Presentation
Gaurav Tyagi
 

More from OUM SAOKOSAL (20)

PPTX
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
PDF
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
PDF
Java OOP Programming language (Part 1) - Introduction to Java
OUM SAOKOSAL
 
PDF
Aggregate rank bringing order to web sites
OUM SAOKOSAL
 
DOC
How to succeed in graduate school
OUM SAOKOSAL
 
PDF
Google
OUM SAOKOSAL
 
PDF
E miner
OUM SAOKOSAL
 
PDF
Data preparation for mining world wide web browsing patterns (1999)
OUM SAOKOSAL
 
PDF
Consumer acceptance of online banking an extension of the technology accepta...
OUM SAOKOSAL
 
DOCX
When Do People Help
OUM SAOKOSAL
 
DOC
Mc Nemar
OUM SAOKOSAL
 
DOCX
Correlation Example
OUM SAOKOSAL
 
DOC
Sem Ski Amos
OUM SAOKOSAL
 
PPT
Sem+Essentials
OUM SAOKOSAL
 
DOC
Path Spss Amos (1)
OUM SAOKOSAL
 
PPT
Kimchi Questionnaire
OUM SAOKOSAL
 
DOC
How To Succeed In Graduate School
OUM SAOKOSAL
 
PPT
Actionscript 3 - Session 7 Other Note
OUM SAOKOSAL
 
PPT
Actionscript 3 - Session 4 Core Concept
OUM SAOKOSAL
 
PPT
Actionscript 3 - Session 3 Action Script And Flash
OUM SAOKOSAL
 
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Java OOP Programming language (Part 1) - Introduction to Java
OUM SAOKOSAL
 
Aggregate rank bringing order to web sites
OUM SAOKOSAL
 
How to succeed in graduate school
OUM SAOKOSAL
 
Google
OUM SAOKOSAL
 
E miner
OUM SAOKOSAL
 
Data preparation for mining world wide web browsing patterns (1999)
OUM SAOKOSAL
 
Consumer acceptance of online banking an extension of the technology accepta...
OUM SAOKOSAL
 
When Do People Help
OUM SAOKOSAL
 
Mc Nemar
OUM SAOKOSAL
 
Correlation Example
OUM SAOKOSAL
 
Sem Ski Amos
OUM SAOKOSAL
 
Sem+Essentials
OUM SAOKOSAL
 
Path Spss Amos (1)
OUM SAOKOSAL
 
Kimchi Questionnaire
OUM SAOKOSAL
 
How To Succeed In Graduate School
OUM SAOKOSAL
 
Actionscript 3 - Session 7 Other Note
OUM SAOKOSAL
 
Actionscript 3 - Session 4 Core Concept
OUM SAOKOSAL
 
Actionscript 3 - Session 3 Action Script And Flash
OUM SAOKOSAL
 

Recently uploaded (20)

PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
GYTPOL If You Give a Hacker a Host
linda296484
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
Doc9.....................................
SofiaCollazos
 
GYTPOL If You Give a Hacker a Host
linda296484
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Software Development Company | KodekX
KodekX
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 

Java OOP Programming language (Part 6) - Abstract Class & Interface

  • 1. Java Programming – Abstract Class & Interface Oum Saokosal Master’s Degree in information systems,Jeonju University,South Korea 012 252 752 / 070 252 752 [email protected]
  • 2. Contact Me • Tel: 012 252 752 / 070 252 752 • Email: [email protected] • FB Page: https://facebook.com/kosalgeek • PPT: http://www.slideshare.net/oumsaokosal • YouTube: https://www.youtube.com/user/oumsaokosal • Twitter: https://twitter.com/okosal • Web: http://kosalgeek.com
  • 3. Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the importance of both.
  • 4. What is an Abstract class? Superclasses are created through the process called "generalization" Common features (methods or variables) are factored out of object classifications (ie. classes). Those features are formalized in a class. This becomes the superclass The classes from which the common features were taken become subclasses to the newly created super class Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world It is an artifact of the generalization process Because of this, abstract classes cannot be instantiated They act as place holders for abstraction
  • 5. Vehicle - make: String - model: String - tireCount: int Car - trunkCapacity: int Abstract superclass: Abstract Class Example In the following example, the subclasses represent objects taken from the problem domain. The superclass represents an abstract concept that does not exist "as is" in the real world. Truck - bedCapacity: int Note: UML represents abstract classes by displaying their name in italics.
  • 6. What Are Abstract Classes Used For? Abstract classes are used heavily in Design Patterns Creational Patterns: Abstract class provides interface for creating objects. The subclasses do the actual object creation Structural Patterns: How objects are structured is handled by an abstract class. What the objects do is handled by the subclasses Behavioural Patterns: Behavioural interface is declared in an abstract superclass. Implementation of the interface is provided by subclasses. Be careful not to over use abstract classes Every abstract class increases the complexity of your design Every subclass increases the complexity of your design Ensure that you receive acceptable return in terms of functionality given the added complexity.
  • 7. Defining Abstract Classes Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object public abstract class Vehicle { private String make; private String model; private int tireCount; [...] public class Car extends Vehicle { private int trunkCapacity; [...] Vehicle - make: String - model: String - tireCount: int Car - trunkCapacity: int Truck - bedCapacity: int public class Truck extends Vehicle { private int bedCapacity; [...] Often referred to as "concrete" classes
  • 8. Abstract Methods Methods can also be abstracted An abstract method is one to which a signature has been provided, but no implementation for that method is given. An Abstract method is a placeholder. It means that we declare that a method must exist, but there is no meaningful implementation for that methods within this class Any class which contains an abstract method MUST also be abstract Any class which has an incomplete method definition cannot be instantiated (ie. it is abstract) Abstract classes can contain both concrete and abstract methods. If a method can be implemented within an abstract class, and implementation should be provided.
  • 9. Abstract Method Example In the following example, a Transaction's value can be computed, but there is no meaningful implementation that can be defined within the Transaction class. How a transaction is computed is dependent on the transaction's type Note: This is polymorphism. Transaction - computeValue(): int RetailSale - computeValue(): int StockTrade - computeValue(): int
  • 10. Defining Abstract Methods Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object public abstract class Transaction { public abstract int computeValue(); public class RetailSale extends Transaction { public int computeValue() { [...] Transaction - computeValue(): int RetailSale - computeValue(): int StockTrade - computeValue(): int public class StockTrade extends Transaction { public int computeValue() { [...] Note: no implementation
  • 11. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in an interface are abstract. Interfaces can contain no implementation Interfaces cannot contain instance variables. However, they can contain public static final variables (ie. constant class variables) • Interfaces are declared using the "interface" keyword If an interface is public, it must be contained in a file which has the same name. • Interfaces are more abstract than abstract classes • Interfaces are implemented by classes using the "implements" keyword.
  • 12. Declaring an Interface public interface Steerable { public void turnLeft(int degrees); public void turnRight(int degrees); } In Steerable.java: public class Car extends Vehicle implements Steerable { public int turnLeft(int degrees) { [...] } public int turnRight(int degrees) { [...] } In Car.java: When a class "implements"an interface, the compiler ensures that it provides an implementationfor all methods definedwithin the interface.
  • 13. Implementing Interfaces A Class can only inherit from one superclass. However, a class may implement several Interfaces The interfaces that a class implements are separated by commas • Any class which implements an interface must provide an implementation for all methods defined within the interface. NOTE: if an abstract class implements an interface, it NEED NOT implement all methods defined in the interface. HOWEVER, each concrete subclass MUST implement the methods defined in the interface. • Interfaces can inherit method signatures from other interfaces.
  • 14. Declaring an Interface public class Car extends Vehicle implements Steerable, Driveable { public int turnLeft(int degrees) { [...] } public int turnRight(int degrees) { [...] } // implement methods defined within the Driveable interface In Car.java:
  • 15. Inheriting Interfaces If a superclass implements an interface, it's subclasses also implement the interface public abstract class Vehicle implements Steerable { private String make; [...] public class Car extends Vehicle { private int trunkCapacity; [...] Vehicle - make: String - model: String - tireCount: int Car - trunkCapacity: int Truck - bedCapacity: int public class Truck extends Vehicle { private int bedCapacity; [...]
  • 16. Multiple Inheritance? Some people (and textbooks) have said that allowing classes to implement multiple interfaces is the same thing as multiple inheritance This is NOT true. When you implement an interface: The implementing class does not inherit instance variables The implementing class does not inherit methods (none are defined) The Implementing class does not inherit associations Implementation of interfaces is not inheritance. An interface defines a list of methods which must be implemented.
  • 17. Interfaces as Types When a class is defined, the compiler views the class as a new type. The same thing is true of interfaces. The compiler regards an interface as a type. It can be used to declare variables or method parameters int i; Car myFleet[]; Steerable anotherFleet[]; [...] myFleet[i].start(); anotherFleet[i].turnLeft(100); anotherFleet[i+1].turnRight(45);
  • 18. Abstract Classes Versus Interfaces When should one use an Abstract class instead of an interface? If the subclass-superclass relationship is genuinely an "is a" relationship. If the abstract class can provide an implementation at the appropriate level of abstraction • When should one use an interface in place of an Abstract Class? When the methods defined represent a small portion of a class When the subclass needs to inherit from another class When you cannot reasonably implement any of the methods