ITE 2132 - Object Oriented Programming Concepts
ITE 2132 - Object Oriented Programming Concepts
ITE 2132 - Object Oriented Programming Concepts
Object in programming
Like every real world object, software objects also have state and behavior.
State of the object is represented by data members or fields and behavior is represented by methods.
Class in programming
Class is act as a blue print or template for creating objects.
It provides state and behavior for its objects.
Java is a pure object oriented language means everything we discuss in java is an object.
Constructor In Java
Constructor
Constructor is a special member of a class which is used to initialize public class ConstructorExample1 {
the state of an object. int num;
It provides the values to the data members at the time of object creation String str;
ConstructorExample1(){
that is why it is known as constructor. System.out.println("Constructor called.");
}
public static void main(String args[]){
Characteristics of constructor: //constructor call
A constructor must have the same name as of its class. ConstructorExample1 obj1 = new
ConstructorExample1();
It is invoked at the time of object creation and used to initialize the
state of an object. //print default values of object properties.
System.out.println("num = " + obj1.num);
It does not have an explicit return type. System.out.println("str = " + obj1.str);
}
}
Types of constructor:
Default or no-argument constructor.
Parameterized constructor.
}
Access Modifier In Java
Access modifiers are keywords used for defining accessibility of classes, methods and data members.
Default
Classes, data members, methods and constructors that are not declared with any access modifier are treated as
default. They can be accessed into all classes within the same package only.
Protected
Data members, methods and constructors that are declared with protected access modifier can be accessed into all
classes within the same package and only in subclasses outside the package.
Classes and interfaces can’t be protected except nested classes.
Public
Classes, data members, methods and constructors that are declared with public access modifier can be accessed
everywhere.
UML- Class and Object
A class called circle is designed as
shown in the following class
diagram. It contains: public class Circle { // Save as "Circle.java"
// private instance variable, not accessible from outside this class
(of the type double) and color (of the // The default constructor with no argument.
type String), with default value of 1.0 // It sets the radius and color to their default value.
public Circle() {
and area of this instance, respectively. // A public method for computing the area of circle
public double getArea() {
return radius*radius*Math.PI;
}
}
OOPs concepts in java
OOPs principles are as follows:
Abstraction.
Abstraction is a way of hiding complexity.
Let us take the example of a car.
We know that if accelerator pressed, speed will increase but don’t know the internal process how speed will
be increased.
Encapsulation.
Encapsulation is a process of wrapping code and data into a single unit.
Let us take an example of a HR in a company.
We communicate through HR not directly with the departments.
HR is acting as a public interface here.
Polymorphism.
Polymorphism means more than one forms.
In java polymorphism is a way in which something behaves differently based on its call. Water can be of in
any form solid, liquid or gas.
Inheritance.
Inheritance is the way of re-usability of code.
Let us take the example of parent and child.
A child inherits the properties of its parent.
OOPs concepts in java
OOPs principles are as follows:
Abstraction.
Abstraction is a way of hiding complexity.
Let us take the example of a car.
We know that if accelerator pressed, speed will increase but don’t know the internal process how speed will
be increased.
Encapsulation.
Encapsulation is a process of wrapping code and data into a single unit.
Let us take an example of a HR in a company.
We communicate through HR not directly with the departments.
HR is acting as a public interface here.
Polymorphism.
Polymorphism means more than one forms.
In java polymorphism is a way in which something behaves differently based on its call. Water can be of in
any form solid, liquid or gas.
Inheritance.
Inheritance is the way of re-usability of code.
Let us take the example of parent and child.
A child inherits the properties of its parent.
OOPs concepts in java
Abstraction in real world:
Abstract is a way of hiding complexity.
Let us take the example of a car.
We know that if accelerator pressed, speed will increase but don’t know the internal
process how speed will be increased.
Abstraction in programming:
Abstract way to show essential details (what) to the user and hide non-essential details
(how). Let us take a simple example:
num = num1*num2;
In the above example we multiply two numbers and store result into new variable.
But what is happening behind the sense?
There are so many things like registers, program counter (PC) etc are involved.
Numbers of operations like PUSH, POP etc are happening.
But these operations are hiding by high level languages we are using for programming.
Abstraction in java is achieved with the help of abstract class and interface.
We will discuss later about abstract class and interface.
OOPs concepts in java
Advantages/Benefits of Abstraction:
1. Only show essential details to end user.
2. Hide complexity.
Capsule:
Capsule refers to a small container which contains a dose of medicine.
Definition of Encapsulation:
Encapsulation is a process of wrapping code and data into a single unit.
Encapsulation in programming:
Encapsulation is the way of declaring the data members as private and providing access to the data members through public methods (getter and setter methods).
As private field can’t be access outside the class that means data is hiding within the class. That’s why encapsulation is also known as data hiding.
Important points:
1. Main Concept behind encapsulation is ‘control over the data’. It is achieved using class and access modifier private, protected, public. Class is act as a container
which contains code and data.
2. Factory pattern and Singleton pattern in Java are based on the concept encapsulation.
Advantages/Benefits of Encapsulation:
1. A read-only (immutable) or write-only class can be made.
2. Control over the data.
3. It helps in achieving high cohesion and low coupling in the code.