ITE 2132_Object oriented programming concepts (1)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Agenda

— Object-oriented programming style


— Object and Class in Java
— Constructor In Java
— Access Modifier In Java
— UML- Class and Object
— OOPs concepts in java
Object-oriented programming style
— OOPs is a style of computer programming which represents concepts as
objects that have state and behavior
— Difference between Object Oriented Programming
and Procedural Programming.
Procedural Programming Object Oriented Programming
1. In Procedural Programming a program is created step 1. In Object Oriented Programming a program is
by step instructional format and instructions are created in a way as real world works.
executed in order.
2. Follow top down approach. 2. Follow bottom to top approach.
3. Less secure because it does not have any proper way 3. Secure because it have proper way of data
of data hiding. hiding.
4. Does not provide code re-usability feature. 4. Provide code reusability feature.
5. Doesn’t provide ability to simulate real-world event 5. Provide ability to simulate real-world event
much more effectively. much more effectively.
6. Slow development. 6. Fast Development.
Object and Class in Java
— Object in Real world
— Every real world object/entity has two characteristics state and behavior.
— Let us take the example of a car.
— Its state is defined by color, current speed etc and behavior is defined by changing speed, applying breaks etc.

— 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 Real world


— Let us take the example of cars.
— There are thousands of cars in existence but all built from the same set of blueprints and therefore contains the
same components.
— In other words your car is an instance (object) and cars is the blueprint (class of objects).

— 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.

— Default or no-argument constructor:


— A constructor with no parameter is known as default or no-argument
constructor.
— If no constructor is defined in the class then compiler automatically
creates a default constructor at the time of compilation.
Constructor In Java
— Why default constructor is used?
— Default constructor is used to provide default values to the object
public class ConstructorExample3 {
properties i.e. to provide default state of an object. int num;
String str;
— If no constructor is defined in the class then compiler automatically
ConstructorExample3(int n, String s){
creates a default constructor at the time of compilation. System.out.println("Constructor called.");
num = n;
— Parameterized constructor: str = s;
}
— A constructor with one or more arguments is known as
parameterized constructor. public static void main(String args[]){
//constructor call
ConstructorExample3 obj1 = new
— Why parameterized constructor is used? ConstructorExample3(10, "test");
— Parameterized constructor is used to provide values to the object
//print values of object properties
properties. System.out.println("num = " + obj1.num);
— By use of parameterized constructor different objects can be System.out.println("str = " + obj1.str);
initialize with different states. }
}

— If a class contains parameterized constructor and default constructor is


needed than default constructor has to be defined explicitly. In this case
compiler will not provide default constructor.
public class ConstructorExample5 {
int num;
boolean isStudent;
Constructor In Java String str;

//Two argument constructor


— Constructor overloading in java ConstructorExample5(int n, String s){
System.out.println("Two argument constructor called.");
— The process of defining more than one num = n;
constructor with different parameters in a class is str = s;
known as constructor overloading. Parameters }
can differ in number, type or order.
//Three argument constructor
ConstructorExample5(boolean boolean1, int n, String s){
— Does a constructor return any value? System.out.println("Three argument constructor called.");
isStudent = boolean1;
— Yes, a constructor implicitly returns the instance num = n;
of the current class. }
str = s;

public static void main(String args[]){


— Difference between constructor and method. //two argument constructor call
ConstructorExample5 obj2 = new ConstructorExample5(10, “test");
Constructor Method //print values of object properties.
System.out.println("isStudent = " + obj2.isStudent);
It has same name as of class. It may or may not have same System.out.println("num = " + obj2.num);
System.out.println("str = " + obj2.str);
Invoked implicitly name as of class.
Must not have any explicit Invoked explicitly. //three argument constructor call
ConstructorExample5 obj3 = new ConstructorExample5(false, 20, “test”);
return type. Must have a return type. //print values of object properties.
System.out.println("isStudent = " + obj3.isStudent);
It is used to initialize the state It is used to show behavior System.out.println("num = " + obj3.num);
of an object. of an object. }
System.out.println("str = " + obj3.str);

}
Access Modifier In Java
— Access modifiers are keywords used for defining accessibility of classes, methods and data members.

— Types of access modifier.


— Private
— Data members, methods and constructors that are declared with private access modifier can be accessed into that
class only.
— A class can have a private constructor but we cannot create an instance of that class from outside the class.
— Classes and interfaces can’t be private except nested classes.

— 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

— Two private instance variables: radius


private double radius;
private String color;

(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 "red", respectively. radius = 1.0;


color = "red";
}
— Two overloaded constructors - a
// 2nd constructor with given radius, but color default
default constructor with no public Circle(double r) {
radius = r;
argument, and a constructor which }
color = "red";

takes a double argument for radius.


// A public method for retrieving the radius
— Two public methods: getRadius() and public double getRadius() {
return radius;
getArea(), which return the radius }

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.

— Difference between abstraction and encapsulation:


Encapsulation Abstraction
1. Encapsulation is a concept for 1. Abstraction is a way to show only
wrapping of data and code into a single essential details to user.
unit. 2. Abstraction is way of hiding
2. Encapsulation is a way of data hiding. complexity.
3. Encapsulation is achieved by access 3. Abstraction is achieved by abstract
modifiers and classes. class and interface.
OOPs concepts in java
— Dictionary meaning of Encapsulation:
— The condition of being enclosed.

— 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 real world:


— 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.

— 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.

You might also like