Oop Concepts
Oop Concepts
net/publication/354035260
CITATIONS READS
0 1,673
1 author:
Naol Getachew
Mattu University
27 PUBLICATIONS 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Naol Getachew on 20 August 2021.
Chapter-5
1) Encapsulation
Encapsulation is a practice to bind related functionality (Methods) & Data (Variables) in a protective
wrapper (Class) with required access modifiers (public, private, default & protected) so that the code can
be saved from unauthorized access by outer world and can be made easy to maintain. 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.
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.
Example:
OOP in Java By: Naol G. 2021
The public methods are the access points to this class' fields from the outside java world. Normally, these
methods are referred as getters and setters. Therefore any class that wants to access the variables should
access them through these getters and setters.
Benefits of Encapsulation:
2) Inheritance
In the real world: We inherit traits from our mother and father. We also inherit traits from our
grandmother, grandfather, and ancestors. We might have similar eyes, the same smile, a different height .
. . but we are in many ways "derived" from our parents. In software: Object inheritance is better defined!
Objects that are derived from other object "resemble" their parents by inheriting both state (fields) and
behavior (methods).
OOP in Java By: Naol G. 2021
Inheritance is also known as „IS-A relationship‟. IS-A is a way of saying: This object is a type of that
object. Advantages of Inheritance are:
Code reusability:- Inheritance automates the process of reusing the code of the super classes in
the subclasses.
o With inheritance, an object can inherit its more general properties from its parent object,
and that saves the redundancy in programming.
Code maintenance:- Organizing code into hierarchical classes makes its maintenance and
management easier.
Implementing OOP:- Inheritance helps to implement the basic OOP philosophy to adapt
computing to the problem and not the other way around, because entities (objects) in the real
world are often organized into a hierarchy.
Inheritance Types
OOP in Java By: Naol G. 2021
Both calv1 and calv2 classes have the add() method in common. Classes often have a lot of
state and behavior in common. Result: lots of duplicate code!
Solution: Inheritance
Inheritance allows you to write new classes that inherit from existing classes. The existing class
whose properties are inherited is called the "parent" or superclass. The new class that inherits
from the super class is called the "child" or subclass. Result: Lots of code reuse!
Make Calv1 superclass and Calv2 sub-class: now via extends keyword add() method is
property of Calv2 also.
OOP in Java By: Naol G. 2021
Finally test our inheritance features in „MyClass’ class as follows where (calv2 inherit the add()
method from calv1).
Super keyword
Super is a reference variable that is used to refer immediate parent class object. Uses of super
keyword are as follows:
Example:
OOP in Java By: Naol G. 2021
Inheritance Rules
Use the extends keyword to indicate that one class inherits from another
The subclass inherits all the nonprivate fields and methods of the superclass
Use the super keyword in the subclass constructor to call the superclass constructor
3) Polymorphism
Polymorphism is the ability of an object to take on many forms. It describes a language‟s ability
to process objects of various types and classes through a single, uniform interface.
Polymorphism in Java has two types: Compile time polymorphism (static binding) and Runtime
polymorphism (dynamic binding). Method overloading is an example of static polymorphism,
while method overriding is an example of dynamic polymorphism. An important example of
polymorphism is how a parent class refers to a child class object. In fact, any object that satisfies
more than one IS-A relationship is polymorphic in nature. For instance, let‟s consider a class
Animal and let Cat be a subclass of Animal. So, any cat IS animal. Here, Cat satisfies the IS-A
relationship for its own type as well as its super class Animal.
OOP in Java By: Naol G. 2021
a. Static Polymorphism
In the above example, there are four versions of add methods. The first method takes two
parameters while the second one takes three. For the third and fourth methods there is a change
of order of parameters. The compiler looks at the method signature and decides which method to
invoke for a particular method call at compile time.
OOP in Java By: Naol G. 2021
b. Dynamic Polymorphism
Suppose a sub class overrides a particular method of the super class. Let‟s say, in the program
we create an object of the subclass and assign it to the super class reference. Now, if we call the
overridden method on the super class reference then the sub class version of the method will be
called.
It should be noted that in the first call to move(), the reference type is Vehicle and the object
being referenced is MotorBike. So, when a call to move() is made, Java waits until runtime to
determine which object is actually being pointed to by the reference. In this case, the object is of
the class MotorBike. So, the move() method of MotorBike class will be called. In the second call
to move(), the object is of the class Vehicle. So, the move() method of Vehicle will be called. As
the method to call is determined at runtime, this is called dynamic binding or late binding.
OOP in Java By: Naol G. 2021
4) Abstraction
Abstraction means hiding lower-level details and exposing only the essential and relevant details
to the users. let's consider a Car, which abstracts the internal details and exposes to the driver
only those details that are relevant to the interaction of the driver with the Car.
The second example, consider an ATM Machine; All are performing operations on the ATM
machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know
internal details about ATM.
Abstract class
An abstract class can be defined as a class declared with the keyword “abstract” and has a
restriction that it cannot be instantiated. The general syntax of an abstract class is given below:
OOP in Java By: Naol G. 2021
Abstract Method
We can use the abstract method by implementing it in a subclass i.e. a class inherits the abstract
class and then implements or provides the code for all the abstract methods declared in the
abstract class by overriding them. Thus it becomes compulsory to override the abstract method in
the subclass. If the abstract method is not implemented in the subclass as well, then we have to
declare the subclass also as “abstract”. The general declaration of the abstract method is:
OOP in Java By: Naol G. 2021
5) Interface
Like a class, an interface can have methods and variables, but the methods declared in an
interface are by default abstract (only method signature, no body). Interfaces specify what a class
must do and not how. It is the blueprint of the class. If a class implements an interface and does
not provide method bodies for all functions specified in the interface, then the class must be
declared abstract. To declare an interface, use interface keyword.
A class that implements an interface must implement all the methods declared in the interface.
To implement interface use implements keyword. Abstract classes may contain non-final
variables, whereas variables in interface are final, public and static.
Example: