0% found this document useful (0 votes)
25 views9 pages

OOP'S

This document provides an introduction to object oriented programming concepts. It discusses problems with procedure oriented programming like lack of reusability and losing control of large code bases. It then defines key object oriented programming concepts like objects, classes, abstraction, encapsulation, inheritance and polymorphism. It provides examples to explain each concept and how they are implemented in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

OOP'S

This document provides an introduction to object oriented programming concepts. It discusses problems with procedure oriented programming like lack of reusability and losing control of large code bases. It then defines key object oriented programming concepts like objects, classes, abstraction, encapsulation, inheritance and polymorphism. It provides examples to explain each concept and how they are implemented in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit 5: Introduction to Object Oriented

Programming
Content
1) Problems in Procedure Oriented Programming
2) What is Object Oriented Programming?
3) What is an Object?
4) What is a Class?
5) Difference between Class and an Object
6) What is an Abstraction in Java?
a. How to achieve an Abstraction in Java?
b. Advantages of an Abstraction
7) What is an Encapsulation in Java?
a. How to achieve an Encapsulation in Java?
8) What is an Inheritance in Java?
9) What is a Polymorphism in Java?

Problems in Procedure Oriented Programming

No Reusability
 The programmer concentrates on a specific task, and writes a set of functions to
achieve it
 When there is another task to be added to the software, he would be writing another
set of functions
 Thus, there will be no reusability of previous functions in most of the cases
Losing control on the code
 Software developed following procedure oriented approach, naturally code size will
also be increased
 When the code size exceeds 10,000 lines and before reaching 100,000 lines,
suddenly at a particular point, programmers were losing control on the code
Procedure oriented approach is unnatural

What is Object Oriented Programming?


1) Object-Oriented Programming is a programming pattern that makes use of objects
and their interactions to design and implement applications
2) Objects are entities that serve as the basic building blocks of an object-oriented
application
3) An object is a self-contained entity with attributes and behaviors

What is an Object?
1) An entity which does exist, has state and behavior is known as an object e.g. chair,
bike, marker, pen, table, car etc.
2) If something does not really exist, then it is not an object e.g. our thoughts,
imagination, plans, ideas etc.,
3) According to System existence means contains memory. So a software object
represents a memory.
4) Software objects also have a state and a behavior. A software object's state is stored
in variables and behavior is shown via methods. So an object contains variables and
methods

What is a Class?
1) It is possible that some objects may have similar properties and actions. Such
objects belong to same category called a ‘class’
2) It is only a logical component and not the physical entity e.g. if you had a class called
“Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc.
3) Its properties (data) can be price or speed of these cars.
4) While the methods may be performed with these cars are driving, reverse, braking
etc.
Relation between Class and Objects

What is the difference between a Class and an Object?


A class a model for creating objects and does not exist physically. An object is anything that
exists physically. Both class and objects contains variables and methods

What is an abstraction in Java?


1) In object-oriented programming abstraction is a process of providing functionality
to the users by hiding its implementation details from them
2) In other words, the user will have just the knowledge of what an entity is doing
instead of its implementation
3) Real life example of Abstraction is ATM Machine; All are performing operations on
the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…
etc. but we can't know internal details about ATM.

Advantages of Abstraction
How to Achieve Abstraction in Java?
1) In Java, we can achieve Data Abstraction using Abstract class and Interface
2) Interface allows 100% abstraction (complete abstraction). Interface allow you to
abstract the implementation completely
3) Abstract class allow 0 to 100% abstraction (partial to complete abstraction) because
abstract class can contain concrete methods that have the implementation which
results in a partial abstraction
What is an Encapsulation?
1) We can define it as Encapsulation is the wrapping up of data and functions (methods
that operate on the data) into a single unit (called class).
2) There is a prohibition for direct access to the data. Functions (that combine with the
data) are the only way to access data. These functions are the member functions or
methods in Java. It basically creates a shield due to which the code or data cannot be
accessed outside the shield.
3) In Java class bind the data with its associated method so class is an example of
encapsulation

Achieving Encapsulation in Java


In order to achieve encapsulation in Java, we have to
1) Declare the variables of a class as private, so that they cannot be accessed directly
from outside the class.
2) Provide setter and getter methods that are declared as public, to view and change
the values of the variables.
What is an Inheritance in Java?
1) The process by which one class acquires the properties (data members) and
functionalities (methods) of another class is called inheritance.
2) In the inheritance the class which is give data members and methods is known as
base or super or parent class.
3) The class which is taking the data members and methods is known as sub or derived
or child class

What is Polymorphism in Java?


1) Polymorphism is the ability for a data or message to be processed in more than one
form. It is a concept by which a single operation can be performed in multiple
different ways.
2) For example, a security guard outside an organization behaves differently with
different people entering the organization. He acts in a different way when the Boss
comes and, in another way when the employees come. When the customers enter,
the guard will respond differently. So here, the behavior of the guard is in various
forms, which depends on the member who is coming.

We can define polymorphism in the context of Object-Oriented Programming as


follows:
3) “The virtue (good future) by which the same action can be performed by objects
of different classes and each object responds in a different way depending on its
class is called Polymorphism”.

Today’s Explanation Notes


How to Initialize State of an Object in Java?
There are three ways by which we can initialize the state of an object. In other words, we
can initialize the value of variables in Java by using three ways. They are as follows:
1) During the declaration
2) By using a reference variable
3) By using a method.
4) By using constructor
5) By using setter methods

During the declaration


 In this mechanism, variables are initialized at declaration time only
 But the problem in this way of initialization is that all objects are initialized with
same data
 This why of initialization is suitable for declaring constants? The reason is that
constants value will not change even though we create several objects
For example,
class Person{
private String name=“Raja”;
privateint age=30;
}
Example Program
package oops;
class Person{
//variables are initialized at declaration time
String name="Ram";
intage=35;
publicvoid talk(){
System.out.println("My Name Is="+name);
System.out.println("My Age Is="+age);
}
}
publicclassPersonDeclaration {
publicstaticvoid main(String[] args) {
Person ram=newPerson();
ram.talk();
Person sita=newPerson();
sita.talk();
}
}

By using a reference variable


In this mechanism variables are initialized by using reference of an object.
Example Program
package oops;
classPersonRef{
String name;
intage;
publicvoid talk(){
System.out.println("My Name Is="+name);
System.out.println("My Age Is="+age);
}
}
publicclassPersonReferene {
publicstaticvoid main(String[] args) {
PersonRefram=newPersonRef();
//using object reference variables are initialized
ram.name="Ram";
ram.age=35;
ram.talk();
PersonRefsita=newPersonRef();
sita.name="Sita";
sita.age=25;
sita.talk();
}
}
What is Variable Shadowing?
When a local variable has the same name as one of the instance variables, the local variable
shadows the instance variable inside the method block.
public class Person{
String name = "John";
int age = 21;
public void show(){
String name = "Roger";//having same name as instance variable
int age = 30;
System.out.println("Name: "+ name);
System.out.println("Age: "+ age);
}
public static void main(String[] args){
Person obj = new Person();
obj.show();
}}
Why use this keyword in java?
 Whenever the formal parameter (variables declared with in a method or
constructor) and data members of the class(instance variables) are similar then jvm
get ambiguity (no clarity between formal parameter and member of the class).To
differentiate between formal parameter and data member of the class, the data
member of the class must be preceded by "this".
 this can be used to invoke current class constructor.
 It can be used to invoke current class method (implicitly)

By using a method argument


In this mechanism instance variable are initialized by method arguments
Example Program
package oops;
classPersonM{
String name;
intage;
publicvoid assign(String name,intage){
this.name=name;
this.age=age;
}
publicvoid talk(){
System.out.println("My Name Is="+name);
System.out.println("My Age Is="+age);
}
}
publicclassPersonMethod {
publicstaticvoid main(String[] args) {
PersonMram=newPersonM();
ram.assign("Ram",35);
ram.talk();
}
}

You might also like