Interface, OOP Concept In Java
ZULAIKHA AFZAL
Four pillars of Object-Oriented-Programming Language In Java
1. Abstraction
Let's suppose you want to turn on the bulb in your room.
What do you do to switch on the bulb. You simply press
tbutton,ton and the light bulb turns on. Right? Notice
that here you're only concerned with your final result,
i.e., turning on the light bulb. You do not care about
the circuit of the bulb or how current flows through the
bulb. The point here is that you press the switch, the
bulb turns on! You don't know how the bulb turned on/how
the circuit is made because all these details are hidden
from you. This phenomenon is known as abstraction.
More formally, data abstraction is the way through which
only the essential info is shown to the user, and all
the internal details remain hidden from the use
2. Polymorphism
• One entity many forms.
• The word polymorphism comprises two words, poly which means
many, and morph, which means forms.
• In OOPs, polymorphism is the property that helps to perform
a single task in different ways.
• Let us consider a real-life example of polymorphism. A woman at
the same time can be a mother, wife, sister, daughter, etc.
Here, a woman is an entity having different forms.
• Let's take another example, a smartphone can work like a camera
as well as like a calculator. So, you can see the a smartphone
is an entity having different forms. Also
3. Encapsulation
[Link] act of putting various components together (in a
capsule).
[Link] java, the variables and methods are the components that
are wrapped inside a single unit named class.
[Link] the methods and variables of a class remain hidden from
any other class.
4.A automatic cold drink vending machine is an example of
encapsulation.
[Link] drinks inside the machine are data that is wrapped
inside a single unit cold drink vending machine.
4. Inheritance
[Link] act of deriving new things from existing things.
[Link] Java, one class can acquire all the properties and
behaviours of other some other class
[Link] class which inherits some other class is known as
child class or sub class.
[Link] class which is inherited is known as parent class or
super class.
[Link] helps us to write more efficient code
because it increases the reusability of the code.
[Link] :
[Link] → E-Rickshaw
8. Phone → Smart Phone
An interface
is a
completely // interface interface Animal {
"abstract public void animalSound();
class" that is // interface method (does not have a
body)
used to group public void run();
related // interface method (does not have a
body) }
methods with
empty
bodies:
• Like abstract classes, interfaces cannot be used to create objects
• Interface methods do not have a body - the body is provided by the "implement"
class
• On implementation of an interface, you must override all of its methods
• Interface methods are by default abstract and public
• Interface attributes are by default public, static and final
• An interface cannot contain a constructor (as it cannot be used to create
objects)
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of
an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one
superclass). However, it can be achieved with interfaces, because the class
can implement multiple interfaces.
Note: To implement multiple interfaces, separate them with a comma (see example
below).
interface FirstInterface
{ public void myMethod(); // interface method }
interface SecondInterface { public void myOtherMethod(); //
interface method }
class DemoClass implements FirstInterface, SecondInterface
{ public void myMethod() { [Link]("Some
text.."); } public void myOtherMethod() {
[Link]("Some other text..."); } } class Main {
public static void main(String[] args) { DemoClass myObj =
new DemoClass(); [Link](); [Link](); }
}
Java Abstraction
Data abstraction is the process of hiding certain details and
showing only essential information to the user.
Abstraction can be achieved with either abstract
classes or interfaces.
The abstract keyword is a non-access modifier, used for classes
and methods:
•Abstract class: is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class).
•Abstract method: can only be used in an abstract class, and it
does not have a body. The body is provided by the subclass
(inherited from).
An abstract class can have both abstract and regular methods:
abstract class Animal {
public abstract void animalSound();
//regular methods
public void sleep() {
[Link]("Zzz");
} }
Why And When To Use Abstract Classes and Methods?
• To achieve security
• Hide certain details and only show the important details of an
object.