Professional Documents
Culture Documents
Prepared by:
Lavan Sabir Belal Mohamed
Supervised by: Salam Ahmed
Introduction 3
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.
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.
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.
Polymorphism
compile-time runtime
polymorphism polymorphism
Run-Time Polymorphism: anytime an object is connected with the functionality at run time,
this is called runtime polymorphism. For example, Method Overriding.
8
Code Example:
public class Shape {
public void shapeside () {
System.out.println("the shape side");
}
}
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