SlideShare a Scribd company logo
1 of 28
Advanced Programming
Abstract Classes and Interfaces
Similarities and Differences between them and the proper use of these concepts in object-oriented
design ( Java)
By: Professor Lili Saghafi
proflilisaghafi@gmail.com
@Lili_PLS
Agenda
● Classes
● Abstract Classes
● Interfaces
● Defender method
● The difference between Abstract Classes and
Interface.
● When we should use each one of them
NOTE:
For the purpose of this short lecture ,we assume
that students are familiar with basic object
oriented programming concepts like classes,
inheritance, overriding methods and Object
Oriented Principles, A.P.I.E. ( Abstraction ,
polymorphism , Inheritance and Encapsulation).
Classes
● A template from which objects are created
(instantiated),
● An object is an instantiation of a class.
● Classes are an expanded concept of data
structures:
● like data structures, they can contain
data members, but they can also
contain functions as members.
● In terms of variables, a class would be
the type, and an object would be the
variable. Concept and actualization
● Classes will also realize interfaces (more on this
later). Classes are defined using either
keyword class or keyword struct. 4
Abstract Classes
● An abstract class is a class definition where we don't provide all the
implementation details. The class has the keyword "abstract" added to it
● We denote abstract classes in our diagrams by making the class name in
italics.
● We can also mark some of the methods as abstract. That means we just
provide the method signature and return type, we don't provide the Java
block that implements the method (no body). We mark the methods with
the keyword "abstract".
● Note the method has no braces, no implementation,and just ends with a
semi-colon.
Abstract Classes
Again we use italics NAMES for abstract methods in our diagrams.
If we declare a class as abstract, the methods may or may not be abstract.
We don't need to have any abstract methods. It would be weird, because what
would be the point, but we can do it.
a class would
be the type,
and an object
would be the
variable.
Abstract Classes
● The opposite isn't true. If we mark any method as abstract, we must mark the
class as abstract.
● We can’t have a method that is abstract and private. We couldn’t access it, and
there’s really no point.
● If you want these for subclasses, you want these to be protected.
● Other than that, there's no difference between an abstract class and a regular
class. Abstract classes differ from classes because abstract classes can't be
instantiated, the functionality isn't complete.If we try, our Java program won't even compile.
Abstract Classes
Like a bread recipe ( template ) that has all the steps, but leaves out what to do in key
parts. Abstract classes give us a way we can create a high altitude bread recipe, and a
low altitude bread recipe. These are implemented as child classes that we can
instantiate.
We use abstract classes when we know the methods,
but we don't know how they would be implemented.
● In subclasses, we left the method blank. It's a much better practice to not
implement the method at all.
There are very few instances where implementing an
empty method in Java is a good idea. The reason is, if
someone were using our class, they wouldn't know
they needed to provide an implementation.
Abstract Classes
● When we mark the method as abstract, anyone using our class has no
choice but to provide an implemented child class.
● Otherwise they can't instantiate the class
This is called compile time safety.
It ensures that any classes that extend our abstract
class must provide the bare minimum functionality
to work.
Another reason to implement abstract
classes is to provide functionality that
we know all subclasses we’ll use.Like
makeDough():void method that all
subclasses will use, so we can
provide the implementation and not to
repeat the code which is universally
bad.
Abstract Classes
● Abstract classes force you to expose your class as inheritable.
● We can't mark an abstract class as final, since to use an abstract class, we
must have a subclass that implements it.
● That might not be a great idea, because we can run into many problems
● In fact with Java 8, we quickly running out of places where we want to use
abstract classes.
● Now we'll see why when we get to interfaces and defender methods.
interface
● An interface describes the behavior or capabilities of a class without
committing to a particular implementation of that class.An interface is a
reference type in Java. It is similar to class. It is a collection of abstract
methods.
● A class implements an interface, thereby inheriting the abstract methods
of the interface. Along with abstract methods, an interface may also
contain constants, default methods, static methods, and nested types.
● As one of the similarities to Abstract class, it is a contract that is used
to define hierarchies for all subclasses or it defines specific set of
methods and their arguments. The main difference between them is
that a class can implement more than one interface, but can only inherit
from one abstract class.
● Writing an interface is similar to writing a class. But a class describes the
attributes and behaviors of an object. And an interface contains
behaviors that a class implements.
● Unless the class that implements the interface is abstract, all the
methods of the interface need to be defined in the class.
11
gold
implements
the valuable
interface it's
realizing the
interface by
agreeing to
conform to
the contract
12
What is interface?
● The base of Tesla driverless cars are very simple. They accept a location, drive us to
the destination, and wake us up when it get there. We really don't care how it gets
done.
● The car just has to be capable of performing three tasks.these car tasks can be
considered as a contract of three methods for a class.
● Set location, drive, and wake the person up. That's what an interface is.
● It's a contract that states any class implementing this interface can be typed as the
interface and it guarantees the methods listed in the interface will be implemented.
interface is a completely abstract class that defines a protocol for object interactions.
● Abstract class does not support Multiple Inheritance whereas an Interface
supports multiple Inheritance.An Interface can only have public members
whereas an abstract class can contain private as well as protected members.
● A class implementing an interface must implement all of the methods defined in the
interface, while a class extending an abstract class need not implement any of the
methods defined in the abstract class. 13
An interface
● When defining an interface, we use the keyword "interface" instead of class. That's pretty much the
biggest difference. Interfaces must be public or package scope. If we don't specify the scope of the
interface, it's protected.
● All variables/ attributes in an Interface are by default – public, static ,final
while an abstract class can have instance variables.The reason is interfaces are
contracts, they can't hold state. If we need state, we should use classes.
● Since it's not possible to have multiple inheritance of classes. An interface on
the other hand can be used when it is required to implement one or more
interfaces.
14
● Sometimes we'll see a constant interface pattern. The idea is that we put a
ton of constants in an interface, and we add the interface where we need it.
It's a bad idea, and it's considered an anti-pattern.
If you ever find yourself wanting to
create a constant interface,
put the constants in a final class not
as an interface . 15
● interfaces are only method declarations, and we do not implement any of the
methods. With some exception that is for later.Methods specified in an
interface are implicitly abstract. It has no body. For example,
public abstract float getInfo();
●
● When we talk about interfaces, we're not talking about the is-a relationship
that we see with inheritance.We say the class implements or does x. We use
the keyword "implements" on our class.
16
● If we use the interface as a type, it points to a class instance that implements
the interface.
● We cannot create an instance of an interface. Sometimes we see interfaces used to describe an
"is-a" relationship as well. It's usually when the interface is used as the type for the class. use the
words "does", "acts like", or "implements" when describing the relationship between interface
concepts.
17
We can create a hierarchy of interfaces. In this case, we're extending the
contract of methods with more methods. In this case, we'll use the
keyword "extends".In Java we usually add the word "-able" after the
name.It's a weird convention,try your best, but if the interface name would
be clearer without the "-able" suffix you can skip it. The other option is to
prefix the interface with the word "Can", as in CanSelfDrive.
Interfaces/ Defender Method
● Recently the people at Java ran into a situation where they were painted into a
corner. Most of their collection classes are defined using interfaces. They
wanted to add functional programming to their interfaces, but if they changed
the interface it would break every class using the interface. The interfaces are
a fundamental part of Java, so it would break other people's code.
● To solve this problem, they added something called default or defender
methods to interfaces.
● Defender methods enable us to add new functionality to interfaces and ensure
binary compatibility with code written for older versions of those interfaces.
18
This means we can provide default functionality for some methods. we use the keyword "default" when
defining the method, and provide the default implementation of the method.Defender methods can lead
to a situation where two interfaces provide a defender method with the same name. In this case, the
code will not compile and we cannot use the two interfaces together.
When we have an interface that extends another interface, we can call any super interface defender
method. We do this using the interface name with the super keyword.
19
● Sometimes you'll see an interface containing no methods at all. It's just an empty interface.
● These are called marker interfaces. They inform the compiler the objects of the classes implementing
the marker interface need to be treated differently.
● An example of this in Java is the Serializable interface. Serializable says this object can
be copied, stored to disk, and then recreated. There are conventions, but there are no methods we are
forced to implement. Java won't let you serialize an object unless you tell the compiler it's ok. The
marker interface does that by checking for the interface type.
20
Interfaces/ Marker Interfaces
● There's one other type of interface. It's called a functional interface. This is an
interface containing exactly one method. It's also marked with something called an
annotation.The annotation isn't required. The annotation lets the compiler know it should enforce
single abstract method inside the interface.
● In Interfaces If you want to add a new feature (method) in its contract, then you MUST
implement those method in all of the classes which implement that interface. However, in the
case of an abstract class, the method can be simply implemented in the abstract class and the
same can be called by its subclass
● Interfaces are slow as it requires extra indirection to find corresponding method in the actual class.
Interfaces requires lots of code writing for all its methods ( features) Abstract classes are fast.
● Interfaces are often used to describe the outer abilities of a class, and not its central identity,
E.g. an Automobile class might implement the Recyclable interface, which could apply to many
otherwise totally unrelated objects. 21
annotation
Interfaces/ Functional Interfaces
Problems with INTERFACES
The difference between Abstract Classes
ABSTRACT CLASS :An abstract class is a partially completed class. It's up to us to
complete the class. when we derive from it. Since it's a class, you can only inherit
from a single class.If we want to add or change behavior, abstract classes force us
to use inheritance.
INTERFACES: Interfaces say we promise to implement the methods listed in the
interface, but there's no limit to the number of interfaces our class will
implement. Our classes are free to use as many interfaces as needed. "does" is a
better relationship word for interfaces. A class usually represents one concept, but it
can "do" many actions.The most common use for interfaces is to hide the details of
a class. Example: If we look at the List interface in Java, it contains many methods
for manipulating a list. Behind the interface curtain, there are many types of
lists.There's an ArrayList, a LinkedList, a Vector and so on. With the list interface,
we're separating what each class does from how it does it.The actual class used
doesn't matter.
22
23
programming to an interface , common use for interfaces is to hide the
details of a class.
● If we need to change the class behind the scenes, say a different class
handles large lists better than the one we're using, we can swap the class
without needing to change much code. This is called programming to an
interface.
24
Difference & Similarities
● There is no difference between a fully abstract class (all methods declared as
abstract and all fields are public static final) and an interface.
a. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple
Inheritance.
b. An Interface can only have public members whereas an abstract class can contain private as well
as protected members.
c. All variables in an Interface are by default – public static final
d. An abstract class can have instance variables.
● Similarities: Neither Abstract classes nor Interface can be instantiated.
● An interface is not extended by a class; it is implemented by a class.
● An interface can extend multiple interfaces.
25
When to use each one of them
● In most cases, we want to avoid inheritance. We should use abstract classes
only when we want to include functionality, but parts of the class's purpose
need to be implemented by the child class.With abstract classes, you are
forcing other developers to implement child classes using inheritance.
● Interfaces are much easier to add to existing classes, and with defender
methods there is little reason to not consider interfaces first.When we have an
interface that extends another interface, we can call any super interface defender method. We do
this using the interface name with the super keyword.
26
Defender methods enable us to
add new functionality to
interfaces and ensure binary
compatibility with code written for
older versions of those interfaces
When to use each one of them
1. Abstract classes are useful in a situation when some general methods should be
implemented and specialization behavior should be implemented by
subclasses. makeDugh case.
2. We use abstract classes when we know the methods, but we don't know how they
would be implemented.
3. Interfaces are useful in a situation when all its properties need to be
implemented by subclasses/ child classes which enforce inheritance .
4. An interface is also used in situations when a class needs to extend another class
apart from the abstract class.
5. If the various objects are all of-a-kind, and share a common state and behavior,
then tend towards a common base class.
6. If all they share is a set of method signatures, then tend towards an interface.
7. Obviously one should ideally go for an interface, as we can only extend one class.
8. Implementing an interface for a class is very much effective rather than extending
an abstract class because we can extend some other useful class for this subclass27
Advanced Programming
Abstract Classes and Interfaces
similarities and differences between them and the proper use of these concepts in object-oriented
design ( Java)
By: Professor Lili Saghafi
proflilisaghafi@gmail.com
@Lili_PLS

More Related Content

What's hot

Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design GuidelinesMohamed Meligy
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented PrinciplesSujit Majety
 
Java Design Pattern Interview Questions
Java Design Pattern Interview QuestionsJava Design Pattern Interview Questions
Java Design Pattern Interview Questionsjbashask
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patternsPrabhakar Sharma
 
Object-Oriented Concepts
Object-Oriented ConceptsObject-Oriented Concepts
Object-Oriented ConceptsAbdalla Mahmoud
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - EncapsulationMichael Heron
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2eeShiva Cse
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation javaJayasankarPR2
 
OOP programming
OOP programmingOOP programming
OOP programminganhdbh
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 

What's hot (20)

Deciphering the Ruby Object Model
Deciphering the Ruby Object ModelDeciphering the Ruby Object Model
Deciphering the Ruby Object Model
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design Guidelines
 
General oops concepts
General oops conceptsGeneral oops concepts
General oops concepts
 
Interfaces
InterfacesInterfaces
Interfaces
 
java - oop's in depth journey
java - oop's in depth journeyjava - oop's in depth journey
java - oop's in depth journey
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
 
Java Design Pattern Interview Questions
Java Design Pattern Interview QuestionsJava Design Pattern Interview Questions
Java Design Pattern Interview Questions
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 
General oop concept
General oop conceptGeneral oop concept
General oop concept
 
Java interface
Java interface Java interface
Java interface
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Object-Oriented Concepts
Object-Oriented ConceptsObject-Oriented Concepts
Object-Oriented Concepts
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - Encapsulation
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
 
OOP programming
OOP programmingOOP programming
OOP programming
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 

Similar to Advanced Programming _Abstract Classes vs Interfaces (Java)

Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docxFredWauyo
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programmingdeffa5
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdfSudhanshiBakre1
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview QuestionsKuntal Bhowmick
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and oodthan sare
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questionssatish reddy
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questionssatish reddy
 
PPT OF JAWA (1).pdf
PPT OF JAWA (1).pdfPPT OF JAWA (1).pdf
PPT OF JAWA (1).pdfNitish Banga
 

Similar to Advanced Programming _Abstract Classes vs Interfaces (Java) (20)

Concepts of oops
Concepts of oopsConcepts of oops
Concepts of oops
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docx
 
JAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdfJAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdf
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
 
Oop
OopOop
Oop
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
OOPS ABAP.docx
OOPS ABAP.docxOOPS ABAP.docx
OOPS ABAP.docx
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
PPT OF JAWA (1).pdf
PPT OF JAWA (1).pdfPPT OF JAWA (1).pdf
PPT OF JAWA (1).pdf
 

More from Professor Lili Saghafi

Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data,  By : Prof. Lili SaghafiArtificial Intelligence and the importance of Data,  By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data, By : Prof. Lili SaghafiProfessor Lili Saghafi
 
Introduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: FundamentalsIntroduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: FundamentalsProfessor Lili Saghafi
 
Software Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili SaghafiSoftware Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili SaghafiProfessor Lili Saghafi
 
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili SaghafiQuantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili SaghafiProfessor Lili Saghafi
 
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi Professor Lili Saghafi
 
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiIntroduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiProfessor Lili Saghafi
 
Introduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili SaghafiIntroduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili SaghafiProfessor Lili Saghafi
 
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili SaghafiCyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili SaghafiProfessor Lili Saghafi
 
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...Professor Lili Saghafi
 
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...Professor Lili Saghafi
 
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili SaghafiMachine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili SaghafiProfessor Lili Saghafi
 
Machine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili SaghafiMachine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili SaghafiProfessor Lili Saghafi
 
What is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili SaghafiWhat is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili SaghafiProfessor Lili Saghafi
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili SaghafiComputer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili SaghafiProfessor Lili Saghafi
 
Data Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili SaghafiData Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili SaghafiProfessor Lili Saghafi
 
Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi Professor Lili Saghafi
 
Data Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili SaghafiData Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili SaghafiProfessor Lili Saghafi
 

More from Professor Lili Saghafi (20)

Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data,  By : Prof. Lili SaghafiArtificial Intelligence and the importance of Data,  By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Ai
AiAi
Ai
 
Introduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: FundamentalsIntroduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: Fundamentals
 
Software Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili SaghafiSoftware Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili Saghafi
 
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili SaghafiQuantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
 
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
 
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiIntroduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
 
Introduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili SaghafiIntroduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili Saghafi
 
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili SaghafiCyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
 
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
 
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
 
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili SaghafiMachine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
 
Machine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili SaghafiMachine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili Saghafi
 
What is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili SaghafiWhat is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili Saghafi
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili SaghafiComputer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
 
Data Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili SaghafiData Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili Saghafi
 
Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi
 
Data Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili SaghafiData Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili Saghafi
 

Recently uploaded

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Advanced Programming _Abstract Classes vs Interfaces (Java)

  • 1. Advanced Programming Abstract Classes and Interfaces Similarities and Differences between them and the proper use of these concepts in object-oriented design ( Java) By: Professor Lili Saghafi proflilisaghafi@gmail.com @Lili_PLS
  • 2. Agenda ● Classes ● Abstract Classes ● Interfaces ● Defender method ● The difference between Abstract Classes and Interface. ● When we should use each one of them
  • 3. NOTE: For the purpose of this short lecture ,we assume that students are familiar with basic object oriented programming concepts like classes, inheritance, overriding methods and Object Oriented Principles, A.P.I.E. ( Abstraction , polymorphism , Inheritance and Encapsulation).
  • 4. Classes ● A template from which objects are created (instantiated), ● An object is an instantiation of a class. ● Classes are an expanded concept of data structures: ● like data structures, they can contain data members, but they can also contain functions as members. ● In terms of variables, a class would be the type, and an object would be the variable. Concept and actualization ● Classes will also realize interfaces (more on this later). Classes are defined using either keyword class or keyword struct. 4
  • 5. Abstract Classes ● An abstract class is a class definition where we don't provide all the implementation details. The class has the keyword "abstract" added to it ● We denote abstract classes in our diagrams by making the class name in italics. ● We can also mark some of the methods as abstract. That means we just provide the method signature and return type, we don't provide the Java block that implements the method (no body). We mark the methods with the keyword "abstract". ● Note the method has no braces, no implementation,and just ends with a semi-colon.
  • 6. Abstract Classes Again we use italics NAMES for abstract methods in our diagrams. If we declare a class as abstract, the methods may or may not be abstract. We don't need to have any abstract methods. It would be weird, because what would be the point, but we can do it. a class would be the type, and an object would be the variable.
  • 7. Abstract Classes ● The opposite isn't true. If we mark any method as abstract, we must mark the class as abstract. ● We can’t have a method that is abstract and private. We couldn’t access it, and there’s really no point. ● If you want these for subclasses, you want these to be protected. ● Other than that, there's no difference between an abstract class and a regular class. Abstract classes differ from classes because abstract classes can't be instantiated, the functionality isn't complete.If we try, our Java program won't even compile.
  • 8. Abstract Classes Like a bread recipe ( template ) that has all the steps, but leaves out what to do in key parts. Abstract classes give us a way we can create a high altitude bread recipe, and a low altitude bread recipe. These are implemented as child classes that we can instantiate. We use abstract classes when we know the methods, but we don't know how they would be implemented. ● In subclasses, we left the method blank. It's a much better practice to not implement the method at all. There are very few instances where implementing an empty method in Java is a good idea. The reason is, if someone were using our class, they wouldn't know they needed to provide an implementation.
  • 9. Abstract Classes ● When we mark the method as abstract, anyone using our class has no choice but to provide an implemented child class. ● Otherwise they can't instantiate the class This is called compile time safety. It ensures that any classes that extend our abstract class must provide the bare minimum functionality to work. Another reason to implement abstract classes is to provide functionality that we know all subclasses we’ll use.Like makeDough():void method that all subclasses will use, so we can provide the implementation and not to repeat the code which is universally bad.
  • 10. Abstract Classes ● Abstract classes force you to expose your class as inheritable. ● We can't mark an abstract class as final, since to use an abstract class, we must have a subclass that implements it. ● That might not be a great idea, because we can run into many problems ● In fact with Java 8, we quickly running out of places where we want to use abstract classes. ● Now we'll see why when we get to interfaces and defender methods.
  • 11. interface ● An interface describes the behavior or capabilities of a class without committing to a particular implementation of that class.An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. ● A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. ● As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface, but can only inherit from one abstract class. ● Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements. ● Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. 11 gold implements the valuable interface it's realizing the interface by agreeing to conform to the contract
  • 12. 12
  • 13. What is interface? ● The base of Tesla driverless cars are very simple. They accept a location, drive us to the destination, and wake us up when it get there. We really don't care how it gets done. ● The car just has to be capable of performing three tasks.these car tasks can be considered as a contract of three methods for a class. ● Set location, drive, and wake the person up. That's what an interface is. ● It's a contract that states any class implementing this interface can be typed as the interface and it guarantees the methods listed in the interface will be implemented. interface is a completely abstract class that defines a protocol for object interactions. ● Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance.An Interface can only have public members whereas an abstract class can contain private as well as protected members. ● A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class. 13
  • 14. An interface ● When defining an interface, we use the keyword "interface" instead of class. That's pretty much the biggest difference. Interfaces must be public or package scope. If we don't specify the scope of the interface, it's protected. ● All variables/ attributes in an Interface are by default – public, static ,final while an abstract class can have instance variables.The reason is interfaces are contracts, they can't hold state. If we need state, we should use classes. ● Since it's not possible to have multiple inheritance of classes. An interface on the other hand can be used when it is required to implement one or more interfaces. 14
  • 15. ● Sometimes we'll see a constant interface pattern. The idea is that we put a ton of constants in an interface, and we add the interface where we need it. It's a bad idea, and it's considered an anti-pattern. If you ever find yourself wanting to create a constant interface, put the constants in a final class not as an interface . 15
  • 16. ● interfaces are only method declarations, and we do not implement any of the methods. With some exception that is for later.Methods specified in an interface are implicitly abstract. It has no body. For example, public abstract float getInfo(); ● ● When we talk about interfaces, we're not talking about the is-a relationship that we see with inheritance.We say the class implements or does x. We use the keyword "implements" on our class. 16
  • 17. ● If we use the interface as a type, it points to a class instance that implements the interface. ● We cannot create an instance of an interface. Sometimes we see interfaces used to describe an "is-a" relationship as well. It's usually when the interface is used as the type for the class. use the words "does", "acts like", or "implements" when describing the relationship between interface concepts. 17 We can create a hierarchy of interfaces. In this case, we're extending the contract of methods with more methods. In this case, we'll use the keyword "extends".In Java we usually add the word "-able" after the name.It's a weird convention,try your best, but if the interface name would be clearer without the "-able" suffix you can skip it. The other option is to prefix the interface with the word "Can", as in CanSelfDrive.
  • 18. Interfaces/ Defender Method ● Recently the people at Java ran into a situation where they were painted into a corner. Most of their collection classes are defined using interfaces. They wanted to add functional programming to their interfaces, but if they changed the interface it would break every class using the interface. The interfaces are a fundamental part of Java, so it would break other people's code. ● To solve this problem, they added something called default or defender methods to interfaces. ● Defender methods enable us to add new functionality to interfaces and ensure binary compatibility with code written for older versions of those interfaces. 18
  • 19. This means we can provide default functionality for some methods. we use the keyword "default" when defining the method, and provide the default implementation of the method.Defender methods can lead to a situation where two interfaces provide a defender method with the same name. In this case, the code will not compile and we cannot use the two interfaces together. When we have an interface that extends another interface, we can call any super interface defender method. We do this using the interface name with the super keyword. 19
  • 20. ● Sometimes you'll see an interface containing no methods at all. It's just an empty interface. ● These are called marker interfaces. They inform the compiler the objects of the classes implementing the marker interface need to be treated differently. ● An example of this in Java is the Serializable interface. Serializable says this object can be copied, stored to disk, and then recreated. There are conventions, but there are no methods we are forced to implement. Java won't let you serialize an object unless you tell the compiler it's ok. The marker interface does that by checking for the interface type. 20 Interfaces/ Marker Interfaces
  • 21. ● There's one other type of interface. It's called a functional interface. This is an interface containing exactly one method. It's also marked with something called an annotation.The annotation isn't required. The annotation lets the compiler know it should enforce single abstract method inside the interface. ● In Interfaces If you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass ● Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Interfaces requires lots of code writing for all its methods ( features) Abstract classes are fast. ● Interfaces are often used to describe the outer abilities of a class, and not its central identity, E.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects. 21 annotation Interfaces/ Functional Interfaces Problems with INTERFACES
  • 22. The difference between Abstract Classes ABSTRACT CLASS :An abstract class is a partially completed class. It's up to us to complete the class. when we derive from it. Since it's a class, you can only inherit from a single class.If we want to add or change behavior, abstract classes force us to use inheritance. INTERFACES: Interfaces say we promise to implement the methods listed in the interface, but there's no limit to the number of interfaces our class will implement. Our classes are free to use as many interfaces as needed. "does" is a better relationship word for interfaces. A class usually represents one concept, but it can "do" many actions.The most common use for interfaces is to hide the details of a class. Example: If we look at the List interface in Java, it contains many methods for manipulating a list. Behind the interface curtain, there are many types of lists.There's an ArrayList, a LinkedList, a Vector and so on. With the list interface, we're separating what each class does from how it does it.The actual class used doesn't matter. 22
  • 23. 23
  • 24. programming to an interface , common use for interfaces is to hide the details of a class. ● If we need to change the class behind the scenes, say a different class handles large lists better than the one we're using, we can swap the class without needing to change much code. This is called programming to an interface. 24
  • 25. Difference & Similarities ● There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface. a. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance. b. An Interface can only have public members whereas an abstract class can contain private as well as protected members. c. All variables in an Interface are by default – public static final d. An abstract class can have instance variables. ● Similarities: Neither Abstract classes nor Interface can be instantiated. ● An interface is not extended by a class; it is implemented by a class. ● An interface can extend multiple interfaces. 25
  • 26. When to use each one of them ● In most cases, we want to avoid inheritance. We should use abstract classes only when we want to include functionality, but parts of the class's purpose need to be implemented by the child class.With abstract classes, you are forcing other developers to implement child classes using inheritance. ● Interfaces are much easier to add to existing classes, and with defender methods there is little reason to not consider interfaces first.When we have an interface that extends another interface, we can call any super interface defender method. We do this using the interface name with the super keyword. 26 Defender methods enable us to add new functionality to interfaces and ensure binary compatibility with code written for older versions of those interfaces
  • 27. When to use each one of them 1. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. makeDugh case. 2. We use abstract classes when we know the methods, but we don't know how they would be implemented. 3. Interfaces are useful in a situation when all its properties need to be implemented by subclasses/ child classes which enforce inheritance . 4. An interface is also used in situations when a class needs to extend another class apart from the abstract class. 5. If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. 6. If all they share is a set of method signatures, then tend towards an interface. 7. Obviously one should ideally go for an interface, as we can only extend one class. 8. Implementing an interface for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass27
  • 28. Advanced Programming Abstract Classes and Interfaces similarities and differences between them and the proper use of these concepts in object-oriented design ( Java) By: Professor Lili Saghafi proflilisaghafi@gmail.com @Lili_PLS