Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Jarvie Joy D.

Robregado

BSIT - 2A

ACTIVITY 1

1. What is Object Oriented Programming?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on the data,
while object-oriented programming is about creating objects that contain both data and functions.

Object-oriented programming has several advantages over procedural programming:

OOP is faster and easier to execute

OOP provides a clear structure for the programs

OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain,
modify and debug

OOP makes it possible to create full reusable applications with less code and shorter development time

2. Principles of OOP (Definitions and Examples)

Encapsulation

Encapsulation is the mechanism of hiding of data implementation by restricting access to public


methods. Instance variables are kept private and accessor methods are made public to achieve this.

For example, we are hiding the name and dob attributes of person class in the below code snippet.

Encapsulation — private instance variable and public accessor methods.

public class Employee {

private String name;

private Date dob;

public String getName() {

return name;
}

public void setName(String name) {

this.name = name;

public Date getDob() {

return dob;

public void setDob(Date dob) {

this.dob = dob;

Abstraction

Abstract means a concept or an Idea which is not associated with any particular instance. Using abstract
class/Interface we express the intent of the class rather than the actual implementation. In a way, one
class should not know the inner details of another in order to use it, just knowing the interfaces should
be good enough.

Inheritance

Inheritances expresses “is-a” and/or “has-a” relationship between two objects. Using Inheritance, In
derived classes we can reuse the code of existing super classes. In Java, concept of “is-a” is based on
class inheritance (using extends) or interface implementation (using implements).

For example, FileInputStream "is-a" InputStream that reads from a file.

Polymorphism

It means one name many forms. It is further of two types — static and dynamic. Static polymorphism is
achieved using method overloading and dynamic polymorphism using method overriding. It is closely
related to inheritance. We can write a code that works on the superclass, and it will work with any
subclass type as well.

Example

Java collections framework has an interface called java.util.Collection, ArrayList and TreeSet are two
different implementation of this interface. ArrayList maintains the insertion order of elements while
TreeSet orders its elements by their natural order or comparator(if supplied). Now if we write a method
that accepts a collection and prints its elements, the actual object (ArrayList or TreeSet) at runtime will
decide the behavior of this method.

Polymorphic print method

public void print(Collection<String> collection) {

for (String s : collection) {

System.out.println("s = " + s);

Passing an ArrayList

Collection<String> collection1 = new ArrayList<>();

collection1.add("A");

collection1.add("D");

collection1.add("B");

collection1.add("C");

print(collection1); (1)

elements will be printed as per the insertion order of elements into arraylist

Program output

s=A

s=D

s=B

s=C

Passing an TreeSet

Collection<String> collection2 = new TreeSet<>();

collection2.add("A");

collection2.add("D");
collection2.add("B");

collection2.add("C");

print(collection2); (1)

elements will be printed as per the natural order

Program output

s=A

s=B

s=C

s=D

We just saw that print() method’s behavior is determined by the actual type of object passed to it at run
time. That’s polymorphism!

3. Define Class and Objects

What is Class?

CLASS are a blueprint or a set of instructions to build a specific type of object. It is a basic concept of
Object-Oriented Programming which revolve around the real-life entities. Class in Java determines how
an object will behave and what the object will contain.

Syntax

class <class_name>{

field;

method;

What is Object?

OBJECT is an instance of a class. An object is nothing but a self-contained component which consists of
methods and properties to make a particular type of data useful. For example color name, table, bag,
barking. When you send a message to an object, you are asking the object to invoke or execute one of
its methods as defined in the class.
From a programming point of view, an object can include a data structure, a variable, or a function. It
has a memory location allocated. The object is designed as class hierarchies.

Syntax

ClassName ReferenceVariable = new ClassName();

What is the Difference Between Object & Class?

A class is a blueprint or prototype that defines the variables and the methods (functions) common to all
objects of a certain kind.

An object is a specimen of a class. Software objects are often used to model real-world objects you find
in everyday life.

4. Types of Methods

1. Static methods: A static method is a method that can be called and executed without creating an
object. In general, static methods are used to create instance methods.

Static method can be invoked directly via class name i.e.; we don't have to create an object for a class in
order to initiate static method.

You can get more information on static methods here.

2. Instance methods: These methods act upon the instance variables of a class. Instance methods are
being invoked with below syntax

<<class-name>>.<<method-name>>;

Instance methods are classified into two types

a. Accessor methods: These are the methods which read the instance variables i.e.; just go access the
instance variables data. Generally these methods are named by prefixing with "get".

b. Mutator method: These are the methods, which not only read the instance variables but also modify
the data. Generally these methods are named by prefixing with "set".

InstanceMethodsDemo.java
/**

* A demo class to explain instance methods

* @author Santhosh Reddy Mandadi

* @since 02-Mar-2012

* @version 1.0

*/

class Person

//instance variables

String name;

int age;

char sex;

//To initialize variables with parameterized constructor

public Person(String name, int age, char sex)

this.name = name;

this.age = age;

this.sex = sex;

public void display()

System.out.println("Name: "+name);

System.out.println("Age: "+age);

System.out.println("Sex: "+sex);

}
Person modify(Person p)

p.name = "Santhosh";

p.age = 27;

p.sex = 'M';

return p;

class InstanceMethodsDemo

public static void main(String args[]) throws Exception

Person p = new Person("Revathi", 23, 'F');

p.display();

Person p1 = p.modify(p);

p1.display();

Person p2 = p1.modify(new Person("Upendar", 25, 'M'));

p2.display();

3. Factory methods: A factory method is a method that returns an object to the class to which it
belongs. All factory methods are static methods.

Eg: NumberFormat obj = NumberFormat.getNumberInstance();


5. Encapsulation In OOP and how does it work?

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes


the idea of bundling data and methods that work on that data within one unit, e.g., a class in Java.

This concept is also often used to hide the internal representation, or state, of an object from the
outside. This is called information hiding. The general idea of this mechanism is simple. If you have an
attribute that is not visible from the outside of an object, and bundle it with methods that provide read
or write access to it, then you can hide specific information and control access to the internal state of
the object.

Encapsulation is an Object Oriented Programming concept that binds together the data and functions
that manipulate the data, and that keeps both safe from outside interference and misuse. Data
encapsulation led to the important OOP concept of data hiding.

Object - it's just a state and behavior. Each class has interface - set of public methods

Encapsulation tells us that we should know just an interface of class to be able to use it. We don't have
to know anything about internals of class to be able to use it.

Let's check following real-world example. The best example of encapsulation could be a calculator. We
understand its interface from first sight and we don't have to know how it works inside. We know that
we can press 2+2 then = and see the result on display.

In Ruby we can easily check interface of class - we should look for its public methods. Then we can find
method that fits for our needs and check which arguments to pass to get result.

In statically typed languages you also have to define a type of arguments which method has as well as
type for returning value.

Ruby it's a dynamically typed language, so all we can learn about method from first sights it's a name
and params it accepts.

References:

https://www.w3schools.com/cpp/cpp_oop.asp

https://medium.com/@cancerian0684/what-are-four-basic-principles-of-object-oriented-programming-
645af8b43727#:~:text=There%20are%204%20major%20principles,pillars%20of%20Object%20Oriented
%20Programming.

https://www.guru99.com/java-oops-class-objects.html

https://www.santhoshreddymandadi.com/java/java-types-of-methods.html
http://rubyblog.pro/2017/01/object-oriented-programming-encapsulation-inheritance

You might also like