You are on page 1of 21

Information and Communication

Technologies (ICT)
Object Oriented Programming

18th Nov 2022


Object-Oriented Programming

● Object-oriented programming differs from traditional procedural


programming by examining the objects that are part of a system. Relies on
the concept of classes and objects.

● It is used to structure a software program into simple, reusable pieces of


code blueprints (usually called classes), which are used to create individual
instances of objects.

● The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.

● There are many object-oriented programming languages including


JavaScript, C++, Java, and Python..
OOPS concepts are as follows:

Abstraction

Encapsulatio
n
Pillars
Inheritance

Oop Class Runtime


Polymorphis
m
Compile-tim
Object
e
Object

An object is a basic unit of Object-Oriented Programming that


represents real-life entities. Objects are persons, places, or things that
are relevant to the system we are analyzing. Typical objects may be
customers, items, orders, and so on.
An object has an identity, state, and behavior.

For example “Dog” is a real-life Object, which has some characteristics


like color, Bark, Sleep, and Eats.
Class

A class is a user-defined blueprint or prototype from which objects are


created. It represents the set of properties or methods that are
common to all objects of one type. Objects are typically part of a
group of similar items called classes. The desire to place items into
classes is not new.
For Example: Consider the Class of Cars.
There may be many cars with different names and brands. All of them
will have 4 wheels, Speed Limit, Mileage range, etc.
So here, Car is the class, and wheels, speed limits, mileage are their
properties.

A method is a collection of statements that


perform some specific task and return the
result to the caller.
Pillars of OOP
Abstraction

Abstraction is the method of hiding the implementation details and


showing only the functionality.
Consider a real-life example:
You know how car look like and how to drive but you don’t know how
to build a car.
In Java, abstraction is achieved by interfaces and abstract classes.
abstract class Bike{
abstract void run(); //abstract method:
no method body and abstract }
class Honda4 extends Bike{
void run()
{System.out.println("running safely");}
public static void main(String args[]) { interface printable{
Bike obj = new Honda4(); void print();
}
obj.run(); } } class A6 implements printable{
public void print()
{System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print(); } }
Encapsulation

● Encapsulation means containing all important information inside


an object, and only exposing selected information to the outside
world.
● Data in a class is hidden from other classes, which is similar to
what data-hiding does.
● For example, you have an attribute that is not visible from the
outside of an object. You bundle it with methods that provide read
or write access. Encapsulation allows you to hide specific
information and control access to the internal state of the object.
Encapsulation requires defining some fields as private and some as public.

● Private/ Internal interface: methods and properties, accessible from other


methods of the same class.
● Public / External Interface: methods and properties, accessible also from
outside the class.

public class Student{


private String name;
public String getName(){
return name; }
public void setName(String name){
this.name=name } }

class Test{
public static void main(String[] args){
Student s=new Student(); //creating instance of the encapsulated class
s.setName("vijay"); //setting value in the name member
System.out.println(s.getName()); //getting value of the name member } }
Inheritance

Classes can have children; that is, one class can be created out of another
class. The capability of a class to derive properties and characteristics from
another class is called Inheritance.

• Superclass: The class whose features are inherited is known as


superclass (also known as base or parent class).

• Subclass: The class that inherits the other class is known as subclass (also
known as derived or extended or child class). The subclass can add its
own fields and methods in addition to the superclass fields and methods.

• Reusability: We can derive our new class from the existing class. By
doing this, we are reusing the fields and methods of the existing class.
Type of Inheritance
class one {
public void print_geek()
{
System.out.println("Geeks");
}
}

class two extends one {


public void print_for() { System.out.println("for"); }
}
// Driver class
public class Main {
public static void main(String[] args)
{
two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Polymorphism

● The word polymorphism


means having many forms. In
simple words, we can define
polymorphism as the ability of
a message to be displayed in
more than one form. For example, A person at the
● Using inheritance, objects can same time can have different
override shared parent characteristics. Like a man at the
behaviors, with specific child same time is a father, a
behaviors. husband, an employee.
So the same person posses
● Polymorphism allows the
different behavior in different
same method to execute
situations. This is called
different behaviors in two
polymorphism.
ways.
In Oop, there are two types of polymorphism as below:

• Static Binding (or Compile time) Polymorphism, e.g., Method


Overloading and Operator Overloading
• Dynamic Binding (or Runtime) Polymorphism, e.g., Method
overriding
● Method Overloading
Compile Time polymorphism uses method overloading.
Methods or functions may have the same name, but a
different number of parameters passed into the method
call. Different results may occur depending on the number
of parameters passed in.

● Java Method Overloading example


class OverloadingExample {
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
● Method Overriding
Runtime polymorphism uses method overriding. In method
overriding, a child class can provide a different implementation than
its parent class.
● The method must have the same name as in the parent class
● The method must have the same parameter as in the parent class.
Java Method Overriding example
class Animal {
void eat(){System.out.println("eating...");}
}
class Dog extends Animal {
void eat(){System.out.println(“Dog is eating...");}
}
*When we create the instance of class Dog and call the eat() method, we see that
only derived class eat() method run instead of base class method eat(), and When
we create the instance of class Animal and call the eat() method, we see that only
base class eat() method run.
Benefits of OOP

OOP models Allows for Secure,


complex class-specific protects
things as behavior information
reproducible, through through
simple polymorphis encapsulatio
structures m n

Reusable, Easier to
OOP objects debug,
can be used classes often
across contain all
programs applicable
information
to them

You might also like