Many occasion students are wondering which one should they choose in coding and why .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.
“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
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
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