You are on page 1of 10

OOPS:

Inheritance:
Inheritance is the concept of a child class (Sub
class) automatically inheriting the variables and
methods defined in a parent class (Super class) .
It is one of the primary features of Object
Oriented Programming.
Benefits:
The primary benefit of inheritance is
reusability.
Once a behavior is defined in a super class, that
behavior is automatically inherited by all its
subclasses and reused.
So developers need not redevelop the logic
again.
Real time Example:
Brad wrote a class for each of the three shapes
Square
Rotate() {
// code to
rotate a square
}

playSound() {
//code to play
the amv file for
square
}
Circle
Rotate() {
// code to
rotate a Circle
}
playSound() {
//code to
play the amv
file for Circle
}
Triangle
Rotate() {
// code to rotate a triangle
}
playSound() {
//code to play the amv file for triangle
}

Implementation:
I found a flaw in Brads approach and Brad got
duplicated code! The rotate procedure is in all
four shape things.
How OO inheritance works, to avoid duplicated
code.
1. I looked at what all three classes have in
common.
Square
rotate()
playSound()
Circle
rotate()
playSound()
Triangle
rotate()
playSound()
2. They are shapes, and they all rotate and
playSound.

So I abstracted out the common features and


put them
Into a new class called Shape.

Shape
rotate()
playSound()
3. Then I linked the other three shape classes to
the new
Shape class, in a relationship called
inheritance.
Shape
rotate()
playSound()

Squar
e

Circle

Triang
le

You can read


this as, Square inherits
from Shape,
Circle inherits from
Shape, and so on. I removed rotate() and
playSound() from other shapes. So now there is
only one copy to maintain.
The Shape class is called the superclass of other
three classes. The other three are the subclasses
of Shape. The subclasses inherit the methods of
the superclass. In other words, If the Shape class
has the functionality, then the subclasses
automatically get that same functionality.
Polymorphism:
Polymorphism is a significant OOP feature in
Java. It enables programmers to create code that
is easier to understand and reuse. Polymorphism
is a powerful feature because it works together

with the other OOP principles to create scalable


programs.
The term polymorphism means many forms. In
Java, polymorphism enables you to define a
single interface for accessing multiple related
behaviors. By using the polymorphism features
in Java, you can create multiple methods that
have the same name but provide different
functionality. The exact method to be executed is
determined either by the compiler or the JVM
depending on the type of polymorphism used.
Real life example:
Suppose if you are in class room that time you
behave like a student, when you are in market at
that time you behave like a customer, when you
at your home at that time you behave like a son
or daughter, Here one person present in
different-different behaviors.

Lets take other example of some words which


can be used in different situations with
different meaning. 'watch' - watch can be
used as a verb as well as a noun. "He is
wearing a watch." In this, it is used as a noun.
"Watch your actions." In this, it is used as a
verb. Thus we see that the same word can be
used in different situations differently. Likewise
same operators or function names are used
with various meanings in various situations.
Encapsulation:
Encapsulation is a process of wrapping of data
and methods in a single unit is called
encapsulation. Encapsulation is achieved in
java language by class concept.

Combining of state and behavior in a single


container is known as encapsulation. In java
language encapsulation can be achieve using
class keyword, state represents declaration of
variables on attributes and behavior
represents operations in terms of method.
Benefits of Encapsulation:
Provides abstraction between an object and
its clients.
Protects an object from unwanted access by
clients.
Example: A bank application forbids
(restrict) a client to change an Account's
balance.
The main benefit of encapsulation is the
ability to modify our implemented code
without breaking the code of others who
use our code. With this feature
Encapsulation gives maintainability,
flexibility and extensibility to our code.
Real World Example:
Every time you log into your email
account( GMail, Yahoo, Hotmail or official mail),
you have a whole lot of processes taking place in
the backend, that you have no control over. So
your password, would probably be retrieved in an

encyrpted form, verified and only then you are


given access. You do not have any control, over
how the password is verified, and this keeps it
safe from misuse.
Encapsulation links the data with the code that
manipulates it. Encapsulation is the technique of
making the fields in a class private and providing
access to the fields via public methods.
If a field is declared private, it cannot be
accessed by anyone outside the class, thereby
hiding the fields within the class. For this reason,
encapsulation is also referred to as data hiding
(not data security).
Abstraction:
Abstraction is a process of hiding the
implementation from the user, only the
functionality is exposed here. So you are aware
only of what the application does, not how it
does it.
Real World Example:

You might also like