Abstraction in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Abstraction in Java

Description:

Learntek is global online training provider on Big Data Analytics, Hadoop, Machine Learning, Deep Learning, IOT, AI, Cloud Technology, DEVOPS, Digital Marketing and other IT and Management courses. – PowerPoint PPT presentation

Number of Views:221
Slides: 14
Provided by: learntek12
Tags: java

less

Transcript and Presenter's Notes

Title: Abstraction in Java


1

Abstraction in Java
2
(No Transcript)
3
Abstraction in Java If you have started to
learn Java then I believe you must have somewhere
come across term called object-oriented
programming or OOPs concept. Now there are four
pillars in Oops i.e., Abstraction, polymorphism,
encapsulation and inheritance. In this article we
will discuss about one of the four pillars of
Oops i.e., Abstraction.
Abstraction basically is the art of hiding
implementation details from user and provide the
user what they want. Lets try to understand with
real world example. Most of us are quite fond of
owning a car. When we go to place order for the
car we are really not interested to understand
very fine details of implementation of each and
every component insider the car engine, Gear box
etc., we leave those technical details and
implementation for manufacturing engineers and
mechanics to understand we are simply interested
in the car so does the manufacturing company. 
4
They are interested to exactly provide us what we
want and hide the fine implementation details
from us. Likewise, there are tons of real-world
examples where abstraction is in play whether
smartphone you are using or smart television you
are watching all have implemented abstraction in
one way or the other. Coming back to Java
programming or any object-oriented programming to
be more precise same principle follows codes
implementation details are hidden and only the
necessary functionality is provided to the
user. There are two ways to achieve abstraction
in java By using interfaces By using abstract
classes
5
Interfaces- Consider a television remote which
only contains functionality to operate a
television and it doesnt serve any other purpose
besides operating the television. You wont be
able to operate a refrigerator with a television
remote. Here remote acts as an interface between
you and the television. It contains all the
necessary functionalities which you require while
hiding the implementation details from you. In
java Interfaces are similar to classes except
they contain empty methods and can contain
variables also. By empty methods it means that
they dont provide any implementation details and
its for the classes or clients to provide the
necessary implementation details for that method
(or methods) when they implement the interface.
6
Syntax -
1.public interface XYZ public void method ()
2.
Example
1.public interface Television Remote public
void turnOnTelevision() public void turnoff
Television() 2.
7
A java class can just use the implements keyword
to implement the interface and provide the
implementation of the methods of the interface.
Example
public class Television implements Television
Remote _at_Override public void
turnOnTelevision() //method implementation
details _at_Override     public  void turnoff
Televisión() //method implementation details

8
Interfaces provide contract specification or sets
of rules for the classes implementing them. They
set rules for classes and tell them what to do
and not to do. In case the class does not provide
implementation for all the methods of the
interface then the class must be declared
abstract. We will cover abstract classes later.
They provide total abstraction which means that
all the methods are empty and field variables are
public static and final by default. Interfaces
serve several features
1 . They provide total abstraction. 2 . They help
to achieve what we call multiple inheritance as
java doesnt support multiple inheritance, but
you can implement several interfaces in one class
and thus it helps to achieve multiple
inheritance. 3 . They help to achieve loose
coupling in design patterns implementation.
9
Abstract classes Abstract classes are just like
normal java class except they use keyword
abstract in front of class declaration and method
declaration. Syntax
public abstract class XYZ public abstract
methodName()
10
For example
public abstract class Automobile public
abstract void engine() public void 
gearBoxGearOne() //method implementation
Abstract classes are created using abstract
keyword and they may have or may not have method
implementation. If a method is declared abstract
then its implementation has to be provided by the
class extending the abstract class. We can have
abstract class without abstract method as well as
they can contain final methods also. A class that
extends the abstract class is a child class for
that abstract class and has to provide
implementation for the abstract method declared
in the abstract class.
11
Example -
public class Car extends Automobile _at_Override
public void engine() //Method implementation

Now question must be arising why we have
interfaces and abstract classes. There are few
key differences worth noticing 1.Interfaces
are implicitly abstract and cannot have
implementations. Abstract classes can have method
implementations.
12
2.Variables of interfaces are final by default.
Abstract classes may or may not have final
variable. 3.Interface methods are public whereas
abstract classes can provide all types of access
modifiers for its members i.e., public,
protected, private. 4.Interface can extend
interface only while classes can implement
multiple interfaces and can extend one class
only. Thus, both abstract classes and interfaces
are used to achieve abstraction and both have
their own importance while designing a java
solution but most preferable choice for most
developers is to use interfaces as they provide
complete abstraction. I hope this article helps
to clear your doubts regarding abstraction.
13
For more Training Information , Contact
Us Email info_at_learntek.org USA 1734 418
2465 INDIA 40 4018 1306
7799713624
Write a Comment
User Comments (0)
About PowerShow.com