2. ABSTRACTION
▰ Abstraction is one of the key concepts of object-oriented
programming (OOP) languages.
▰ Its main goal is to handle complexity by hiding unnecessary
details from the user. That enables the user to implement more
complex logic on top of the provided abstraction without
understanding or even thinking about all the hidden complexity.
▰ In Java programming, abstraction is achieved using Abstract
classes and interfaces.
3. WHAT IS ABSTRACTIONS?
▰ Abstraction refers to the act of representing essential features without including the
background details or explanations.
▰ Abstraction lets you focus on what the object does instead of how it does it.
▰ Therefore, it is an art to simplify things from outsiders.
▰ The abstraction in Java can be achieved by:
▻ Abstract Class : partial or total abstraction
▻ Interface : total abstraction
▰ Since classes use the concept of data abstraction, they are known as Abstract Data Types
(ADT).
4. WHY ABSTRACTIONS?
▰ easier to think about - hide what doesn't matter
▰ protection - prevent access to things you shouldn't see
▰ plug compatibility
▻ replacement of pieces, often without recompilation,
definitely without rewriting libraries
▻ division of labor in software projects
5. Abstraction vs Encapsulation
▰ Abstraction = Implementation Hiding.
hiding the details and implementation of the code from
outside world.
▰ Encapsulation = Information Hiding.
hiding the hiding the data and controlling the visibility of the
code.
6. Abstract Classes
▰ A class declared as abstract is known as abstract class.
▰ An abstract class must be declared with an abstract keyword.
▰ It cannot be instantiated.
▰ It needs to be extended to be used.
▰ It must contain at least 1 abstract method declaration.
▰ It may contain constructors, non-abstract, static and/or final
methods as well. Otherwise, fully abstraction concept will be
achieved.
7. Declaring an Abstract Class
▰ To declare an abstract class, you simply use the abstract keyword
before the class keyword.
8. Declaring an Abstract Class
▰ To declare an abstract class, you simply use the abstract keyword
before the class keyword. Here’s a basic example:
In this code snippet, we’ve declared an abstract class Vehicle with an abstract
method run(). This method doesn’t have a body – it’s just a declaration. That’s
because the implementation of this method is provided by the class that extends
this abstract class
9. Syntax of Abstract Method
▰ The syntax for the abstract method is similar to any user-defined
method in Java except you have to add abstract keyword at the
start of the declaration.
▰ For example
10. Abstract Class in Java
To make an abstract class, you must declare a class with an abstract
keyword.
11. An abstract class may have a mixed set of abstract and regular(non-abstract)
methods.
Abstract Class in Java
12. When a regular class extends an abstract class, it must implement
the abstract methods of an abstract class, or a compile error is
issued.
Abstract Class in Java
13. You can even create an abstract class which only contains concrete
regular methods and no abstract methods.
Abstract Class in Java
14. You must declare a class with the abstract keyword, even when there
is only a single abstract method in it.
Abstract Class in Java
15. Abstract Class in Java
Example
This example creates an abstract class
called "Shapes"
16.
17.
18.
19. This example creates an abstract class Animal with abstract
methods eat() and sleep(). Create subclasses Lion, Tiger, and
Deer that extend the Animal class and implement the eat() and
sleep() methods differently based on their specific behavior.
Abstract Class in Java
Example
20. ▰ In this program, the Animal class is an abstract class that
defines the abstract methods eat() and sleep().
▰ The Lion, Tiger, and Deer classes extend the Animal class and
provide their own implementations for the eat() and sleep()
methods based on their specific behavior.
▰ The Main class demonstrates the usage of these classes by
creating objects of each subclass and invoking the eat() and
sleep() methods accordingly.
21.
22.
23.
24.
25.
26.
27. INTERFACE
▰ An interface is a 100% abstract class which is declared with
the interface keyword. Methods of an interface are implicitly
abstract, hence when declaring an interface, you must not
provide an implementation of any of its methods and its
methods should end with a semicolon ;
28. ▰ Interface methods are implicitly declared with the public access
modifier and abstract keyword, which means that even if you
don't explicitly type public and abstract with interface methods,
they are still always public and abstract, by default.
INTERFACE
29. ▰ A class can implement an
interface by providing the
internal logic of all methods of
an interface, conforming to
each of their method
signatures. To implement an
interface, a class must use the
implements keyword.
INTERFACE
30. ▰ You cannot instantiate or create an object of an interface, because it is a 100%
abstract and skeletal. Hence, instantiating an interface gives a compile error.
INTERFACE
31. ▰ A class can
implement multiple
interfaces and it
must provide an
implementation of
all their methods.
INTERFACE
32. ▰ An abstract class
can implement
an interface and
it doesn't have to
implement its
methods.
INTERFACE
34. This example create an interface Shape with the getArea()
method. Create three classes Rectangle, Circle, and Triangle that
implement the Shape interface. Implement the getArea() method
for each of the three classes.
Interface in Java Example
35.
36.
37.
38.
39.
40.
41.
42.
43. Exercises
▰ Write a Java program to create an abstract class Shape3D with
abstract methods calculateVolume() and
calculateSurfaceArea(). Create subclasses Sphere and Cube
that extend the Shape3D class and implement the respective
methods to calculate the volume and surface area of each
shape.