You are on page 1of 9

Government of Kurdistan region

Ministry of Higher Education and Scientific Research


University of Garmian
College of Science
Computer Science
First Stage

OBJECT ORIENTED PROGRAMMING

Prepared by:
Lavan Sabir Belal Mohamed
Supervised by: Salam Ahmed

Friday, May 28, 2021


Contents

 Introduction 3

 Object-oriented programming (OOP) 4

 Abstraction 4

 Encapsulation 4

 Inheritance 6

 Polymorphism 8

 References 9

2
Introduction
In this repost I will explain object-oriented programming structure in Java how is it
different from the procedural approach. Object-oriented programming this is one of the software
design methodologies that how we going to write a software solution in the industry and it's
followed widely so what is this object-oriented programming?
it's a methodology or a paradigm using which we can design our software solutions now
there are two major words which you need to focus on the first one is object and the second one
is class now why object comes first in class later because of the term itself object oriented not
class oriented. I will discuss these objects and classes.
object is a real-world entity, it means anything which you can see, touch and feel is an object for
example laptop, mobile phone, chair, table, fan or anything even humans are objects. every
object will have property and behavior.
class is a blueprint or I can say it's a drawing of an object. So how an object will look like how
object is going to be represented. Each class has two things which first one is properties and the
second on his behavior now properties are also referred to as attributes
Person
Attribute Behavior or Method
Name Talk ()
Age Walk ()
Height Lough ()
Eye color Run ()
Skin color Sleep ()

The table represents a class Person which has attributes and behaviors. We can create as
many objects as we want from the Person class as follows:
Person
Attribute Behavior or Method
Name = lavan Talk ()
Lavan Age = 20 Walk ()
Height = 179 Lough ()
degree = high school Run ()
Skin color = pearly white Sleep ()
Elyas of the right side on the table is the name of the object which has been created using
Person class, we assigned the attributes of the class in accordance to the object specifically.
Person
Attribute Behavior or Method
Name = belal Talk ()
Belal Age = 19 Walk ()
Height = 175 Lough ()
degree = high school Run ()
Skin color = brownish Sleep ()

3
I created another object Dler using the same class Person and I can do the same as many
as I want. Having object-oriented programming approach, we can have different type of objects
so the properties will remain same but the value for their properties will change.

Object-Oriented Programming (OOP)


Now there are four major fundamentals when it comes to object-oriented programming
structure we got:
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
These are the four major building blocks of any object-oriented programming language right so
we even call them the pillars for the OOPs so we'll discuss them one by one to begin with
Abstraction.
Abstraction means to simplify reality. For example, a person is an object, but if you were
designing a new application to process data about a person it's unlikely that you'd be interested in
everything there is to know about a person rather you would only concern yourself with the data
that are relevant and the tasks that you want to perform with those data to create objects
programmatically you need a class. Property values that have been assigned to an object are
collectively known as the state of the object it's also possible to assign values to properties while
an object is being instantiated by means of a special method called new this method is known as
the constructor.

Encapsulation
encapsulation is like a capsule the whole code and data is bounded together into a single unit.
another way to think about encapsulation is it is a protective shield that prevents the data from
being accessed by the code outside this shield, as you can see variables and methods are defined
inside one class like a capsule. Now, the question is why do we need encapsulation?
the main reasons are first better control of class attributes and methods, second class attributes
can be made read-only or write-only, third it makes Java code very flexible. this means the
programmer can change one part of the code without affecting other parts. A part from these in
encapsulation is mainly used to protect data and increase security of the code, as you can see to
protect the data variables must be private, and they can only be accessed through methods.

4
Encapsulation in Java is a mechanism of wrapping the data variables and code acting on the data
methods together as single unit. In encapsulation the variables of a class will be hidden from
other classes, and can be accessed only through the methods of their current class, therefore it is
also known as data hiding. now let's see Java code for encapsulation to understand the concept
better.
public class EncapTest{
private String idNumber;
private String name;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getIdNumber(){
return idNumber;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setIdNum( String newId){
idNumber = newId;
}
}
The public set and get methods are the access points of the instance variables of the EncapTest
class. 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 accessed as below
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName("Lavan");
encap.setAge(20);
encap.setIdNumber("12345lava");
System.out.print("Name: " + encap.getName() + " Age: " + encap.getAge());
}
}

Benefits of Encapsulation:
 Flexible programs – We saw in the previous Java programs how we can implement
polymorphism by using getter and setter methods, this method is actually very flexible.
For example, if we want the variables to be read-only we can delete the setter methods
i.e, delete the “setCourse” method from the example above. Similarly, if you want to

5
make the variables as write-only, then you need to delete the getter methods from the
example above.
 Easy debugging and testing – The code which is encapsulated is easy to debug and
enables unit testing.
 Reusability – You can reuse the code and implement new requirements in your program.
 Hiding Data – You can hide the implementation of the program definitions. For
example, the user will never be able to know what variables are being stored in the
program. They will interact with the getter and setter methods of the program.

Inheritance
As we know that java is one of the object-oriented programming languages (OOP in
short), one of the concepts that OOP relies on, is inheritance. So, what is inheritance?
Imagine that we have a tech company that concerns making products like gaming mice,
keyboards, hard drives …etc. We want to make several mouse types; each has a unique spec, like
RGB color, added buttons and/or Bluetooth connection.
But before we make this separation, we should make our generic mouse, which has a left click,
right click and scrolling wheel. Now we are applying this method to a real program and we
should be able to see the power of inheritance.

Creating a generic mouse


In order to have several types of mice, first
we should make an original one, this is the creation
of the generic mouse which consists of right click,
left click and scrolling wheel:

Creating Several branches


Now that we have the generic mouse, we can customize it the way we want, of course we
can re-write those three methods for each different type, but that doesn’t help, our company’s
plan is to make 100 different types of mice! We can’t re-write the same code over and over;
throughout time it becomes more and more complicate to understand. So, fortunately we just
have a great solution, which is inheritance. By inheriting those three methods from the generic
mouse to all other types we solve the complexity. With the code it becomes a lot easier, let’s take
a look:

6
These three above pictures show us couple of things; first, every class has it is own
mouse properties, second one which caught our attention is keyword ‘extends’. Extends is the
keyword we use to identify java that we are trying to inherit something. Inheritance usage is
really easy, we just type extends and the name of the class that we want to inherit from, that’s all.
Now we create our main class and method to output our final result through objects of the
classes. We should be able to see the three main things in every mouse along their special
property.

Final result

7
Polymorphism
Polymorphism in Java is a concept by which we can perform a single action in various forms,
Polymorphism is based on 2 Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So، polymorphism means many forms.

Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.

We have two types of polymorphism in Java:

Polymorphism

compile-time runtime
polymorphism polymorphism

We can perform polymorphism in java by method overloading and method overriding.

Run-Time Polymorphism: anytime an object is connected with the functionality at run time,
this is called runtime polymorphism. For example, Method Overriding.

Compile Time Polymorphism: anytime an object is connected with their functionality at the


compile-time, this is known as the compile-time polymorphism . For example, Method
Overloading.

8
Code Example:
public class Shape {
public void shapeside () {
System.out.println("the shape side");
}
}

class rectangle extends Shape {


public void shapeside () {
System.out.println("the traingle is : 3 side");
}
}

class square extends Shape {


public void shapeside () {
System.out.println("the square is : 4 sides");
}
}

References

 https://www.w3schools.com/java/java_encapsulation.asp
 https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/
 https://www.youtube.com/watch?v=itddyPJs1jo
 https://beginnersbook.com/2013/03/polymorphism-in-java

You might also like