Title: Abstraction in Java
1Abstraction in Java
2(No Transcript)
3Abstraction 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., Online Core Java
Training, Classes,Tutorial, Courses
4Abstraction
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.
They are interested to exactly provide us what we
want and hide the fine implementation details
from us.
5Likewise, 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
6Interfaces- 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.
7Syntax -
1.public interface XYZ public void method ()
2.
Example
1.public interface TelevisionRemote public void
turnOnTelevision() public void
turnOffTelevision() 2.
A java class can just use the implements keyword
to implement the interface and provide the
implementation of the methods of the interface.
8Example
public class Television implements
TelevisionRemote _at_Override public
void turnOnTelevision() //method
implementation details _at_Override
public void turnOffTelevision()
//method implementation details
9Interfaces 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
10It also helps creators solve the payments issue.
Platforms such as Patreon are focused on helping
creators raise money from their fans, these
platforms are still in the middle as a
facilitating platform. The use of
crypto-currencies helps eliminate any and all
middlemen by securing payment and reducing
transaction costs, freeing up a greater portion
of each sale for the creator, points out
this Forbes report2. For music distributors, the
blockchain would make it simpler to honour
licensing agreements and value artists
contributions.
111.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. Learn
Core Java with Learntek
12Now 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. 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.
13For more Training Information , Contact
Us Email info_at_learntek.org USA 1734 418
2465 INDIA 40 4018 1306
7799713624