• Tidak ada hasil yang ditemukan

Basics of Object Oriented Programming FCITR MAGAZINE

N/A
N/A
Protected

Academic year: 2025

Membagikan "Basics of Object Oriented Programming FCITR MAGAZINE"

Copied!
10
0
0

Teks penuh

(1)

Basics of

Object Oriented Programming FCITR MAGAZINE

DR. SHAKEEL AHMAD

FACULTY OF COMPUTING AND INFORMATION TECHNOLOGY AT RABIGH KING ABDULAZIZ UNIVERSITY KINGDOM SAUDI ARABIA

(2)

Introduction

Objects are used to model real world entities in Object-Oriented Programming (OOP) paradigm. Object oriented paradigm organizes software product as a collection of objects that supports strong connection between data and behaviour. Whereas conventional functional or imperative programming paradigm supports loose connection between data and behaviour. Since its inception in 1980, OOP is considered the most powerful programming paradigm for the creation of the software products. The OOP paradigm strongly supports the concepts of Modularization, where the complex system is decompose into small manageable pieces and Software Reuse, where an already developed module can be reused to develop new application to save the development time and cost[1].

Brief History of OOP

A language developed for simulations known as SIMULA was the first object language developed in 1960’s. Then in 1970’s, Smalltalk was developed by a team lead by Alan Kay at Xerox Parc known as the first true object oriented programming language[1]. Smalltalk also exits today but not widely used. Gradually the knowledge of Object Orientation gained popularity and in early 1980, when Bjorn Stroustrup developed C++ by assimilating Object Orientation into C language. The C++ was the first most popular widely used OOP language.

Later at Sun in early 1990, a group headed by James Gosling developed Java known to be a simpler version of C++ and was mainly developed for programming Internet applications.

OOP-Building Blocks

In OOP, programming approach is mainly based on classes and objects. However, the main building blocks of OOP are:

 Classes

 Objects

 Abstraction and Encapsulation

 Inheritance

 Polymorphism

(3)

Classes and Objects

Class definition by Google is “Classes act as templates from which an instance of an object is created at run time”. Classes in general, describe the methods used to control the behavior of object and its properties. In java, “Class is an abstract data type that supports inheritance”[2].

In our daily life, Objects are scattered around us in terms of animals, plants, humans, buildings, vehicles etc. All objects have attributes and behaviour. For example, account attributes/states may include account_type, account_holder, account_balance etc. Whereas account behaviour may include withdraw, deposit, suspend/dead etc. Software objects on the other side also have the attributes and behaviour; attributes are modeled in terms of data structures whereas behaviors are represented by methods. Software objects interact with each other by sending messages.

Figure-1[3]: OOP, Classes and Objects

Java code to create a simple class and object:

class Book {

(4)

String bookTitle;

void setTitle(String title) {

//java code for setting book title }

String getTitle() {

//java code for getting book title }

public static void main(String[] args) {

Book java=new Book(); // keyword new is use to create instance of class }

}

Abstraction and Encapsulation in Brief

Consider the example of remote control, which may be used to change the channels of television or thermostat of an air-conditioner. In this context, remote control is an object with a number of features (attributes) and operations (behaviors) encapsulated inside and are kept completely hidden to its user. User just press some button to perform some task without knowing an internal details and an understanding of those hidden attributes plugged in terms of microchips, wiring, etc. User expects about a button he pressed will perform that specific task. User interaction with remote control in this context is “Abstract” i.e. doing something without digging into the internal details and implementations of the task being performed[1]. This is the main feature, on which the foundation of OOP is laid down which emphasizes on how the objects behave without being going into the internal details of code implementation.

(5)

Figure-2[3]: Abstraction and Encapsulation

Java code to achieve effect of Encapsulation is:

public class FCITR_Student{

private String name;

private int id;

public String get_Student_Name() { return name;

}

public int get_Student_Id() { return id;

}

public void set_Student_Name(String name) { this.name = name;

}

public void set_Student_Id( int id) { this.id = id;

} }

(6)

Java code to achieve effect of Abstraction by means of abstract class is:

public abstract class FCITR_Employee { private String emp_Name;

private String emp_Address;

private int emp_Id;

public abstract double calculateSalary();

}

Java code to achieve effect of Abstraction by means of interface is:

interface FCITR_Employee {

public double calculateSalary();

}

Inheritance

Class Inheritance in OOP languages is “The process of creating a new class from already existing class”[4]. In this context, new class is known as the child/sub/derived class whereas the class from which new class is created is known as parent/super/base class. In this mechanism, the child class acquires the attributes and operations of its parent class. In addition to the attributes (fields) and operation (methods) of parent class, you can also add new fields and methods to the child class. Through class inheritance, information can be managed in hierarchical order.

In Class Inheritance, Parent-Child relationship is represented by “IS-A”

relationship[4]. For example, “Rose IS-A flower” representing flower as parent class to rose class or “Samba IS-A bank” representing bank as parent class to Samba class.

Java code to achieve the effect of class inheritance is:

class Base { ...

...

}

class Derived extends Base { ...

(7)

...

}

In Java code given below, FCITR_Employee is the superclass and FCITR_Librarian is the sub class showing IS-A relationship i.e. Librarian IS-A kind of Employee.

class FCITR_Employee{

float annual_Salary=50000;

}

class FCITR_Librarian extends FCITR_Employee{

int annual_Bonus=12000;

public static void main(String args[]){

FCITR_Librarian ahmad=new FCITR_Librarian();

System.out.println("Ahmad annual salary is: "+ ahmad.annual_Salary);

System.out.println("Ahmad annual bonus is: "+ ahmad.annual_Bonus);

} }

Types of Inheritance

In Class Inheritance hierarchy, there are five types of Inheritance as shown by the following figures [4, 5]:

Single Level

Multilevel

Hierarchical

Multiple

Hybrid

(8)

Java supports only single level, multilevel and Hierarchical types of inheritance whereas the effect of multiple and hybrid inheritance can be achieved through implementations of interfaces[6].

Java code to achieve effect of single level inheritance:

class MotorBike{

//code for MotorBike class }

class Honda_Bike extends MotorBike{

//code for Honda_Bike class }

Java code to achieve effect of multilevel inheritance:

class MotorBike{

//code for MotorBike class }

class Honda_Bike extends MotorBike{

//code for Honda_Bike class }

class Scrambler extends Honda_Bike{

//code for Scrambler class }

Java code to achieve effect of hierarchical inheritance:

class MotorBike{

//code for MotorBike class }

class Honda_Bike extends MotorBike{

//code for Honda_Bike class

(9)

}

class Suzuki extends MotorBike{

//code for Suzuki class }

Polymorphism

The word Polymorphism is the mixture of two Greek words i.e. Poly and Morph means many forms. In this context, the Polymorphism is the process of performing a single task in different ways. In java, the effect of polymorphism can be achieved by means of upcasting and dynamic method dispatch[6].

In upcasting reference variable of base class refers to the object of derived class and in dynamic method dispatch, call to an over-ridden method is resolved at run time.

Java code to achieve effect of Polymorphism:

abstract class MotorBike{

abstract void ride();

}

class Honda_Bike extends MotorBike{

void ride(){System.out.println("It’s fun to have Honda ride");

}

class Suzuki_Bike extends MotorBike{

void ride(){System.out.println("It’s fun to have Suzuki ride");

}

class Polymorphism_Demo{

public static void main(String args[]){

MotorBike mb = new Honda_Bike(); //upcasting

mb.ride(); //Dynamic method dispatch mb=new Suzuki_Bike (); //upcasting

(10)

mb.ride(); //Dynamic method dispatch }

}

References:

1. https://en.wikipedia.org/wiki/Object-oriented_programming 2. https://en.wikibooks.org/wiki/Object_Oriented_Programming

3. https://www.upwork.com/hiring/development/object-oriented-programming/

4. https://www.javatpoint.com/inheritance-in-java#inheritancetypes 5. https://www.tutorialspoint.com/java/

6. https://docs.oracle.com/javase/tutorial/java/concepts/

Referensi

Dokumen terkait