[go: up one dir, main page]

0% found this document useful (0 votes)
10 views14 pages

Oop Concepts

Chapter 5 of the document discusses key Object-Oriented Programming (OOP) concepts in Java, including encapsulation, inheritance, polymorphism, abstraction, and interfaces. It explains how encapsulation protects data, inheritance promotes code reuse, polymorphism allows objects to take on multiple forms, and abstraction simplifies complex systems. Additionally, it covers the structure and implementation of abstract classes and interfaces in Java.

Uploaded by

m8487629
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Oop Concepts

Chapter 5 of the document discusses key Object-Oriented Programming (OOP) concepts in Java, including encapsulation, inheritance, polymorphism, abstraction, and interfaces. It explains how encapsulation protects data, inheritance promotes code reuse, polymorphism allows objects to take on multiple forms, and abstraction simplifies complex systems. Additionally, it covers the structure and implementation of abstract classes and interfaces in Java.

Uploaded by

m8487629
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/354035260

Chapter-5 OOP Concepts in Java

Presentation · August 2021


DOI: 10.13140/RG.2.2.18720.71686

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.

The user has requested enhancement of the downloaded file.


OOP in Java By: Naol G. 2021

Chapter-5

OOP Concept in Java

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.

The variables of the EncapTest class can be access as below:

Benefits of Encapsulation:

 The fields of a class can be made read-only or write-only.


 A class can have total control over what is stored in its fields.
 The users of a class do not know how the class stores its data.
 A class can change the data type of a field and users of the class do not need to change any of
their code.

2) Inheritance

Inheritance is a fundamental feature of object-oriented programming which enables the programmer to


write a class based on an already existing class. The already existing class is called the parent class, or
superclass, and the new class is called the subclass, or derived class. The subclass inherits (reuses) the
non-private members (methods and variables) of the superclass, and may define its own members as well.
Inheritance is implemented in Java using the keyword extends. When class B is a subclass of class A, we
say B extends A.

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

Syntax of Single level inheritance: Syntax of Multi-level inheritance:

Syntax of hierarchy Inheritance:


OOP in Java By: Naol G. 2021

Let us look at why we need inheritance from below implementations:

Example: calv1, calv2 classes

Problem: Code Duplication

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:

 Super() is used to invoke immediate parent class constructors


 Super is used to invoke immediate parent class method
 Super is used to refer immediate parent class variable

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 Java, static polymorphism is achieved through method overloading. Method overloading


means there are several methods present in a class having the same name but different
types/order/number of parameters. At compile time, Java knows which method to invoke by
checking the method signatures. So, this is called compile time polymorphism or static binding.

Example of 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.

Example Dynamic Polymorphism

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

Features of abstract class:

Abstract Method

An abstract method is a method preceded by an „abstract‟ keyword without any implementation.


An abstract method is declared inside an abstract class. An abstract method is the one that makes
a class incomplete as it doesn‟t have an implementation. Hence when we include an abstract
method in the class, naturally the class becomes incomplete.

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

Example-1: Car type and their types

Example-2: Banking System


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:

View publication stats

You might also like