You are on page 1of 33

OBJECT-ORIENTED

CONCEPTS
Procedural versus OO Programming

Procedural Programming – focus is to break down a


programming task into a collection of variables, data
structures, and subroutines
- can sometimes be used as a synonym for imperative
programming(specifying the steps the program must take to
reach the desired state)
- Java, C++,C, Visual Basic, Visual FoxPro
Object-Oriented programming - is to break down a programming
task into classes with each "class" encapsulating its own
methods (subroutines)
- Java, C++,Ruby, SmallTalk, PHP
Procedural versus OO Programming

Note: Writing object-oriented programs involves creating class,


creating objects, and creating applications, which are stand-
alone executable programs that use those objects.
*** After being created, classes can be reused over and over
again to develop new programs.
• Simula – developed in 1967
– was the first OOP language
– Introduced objects as a formal concept in programming
– Developed by Ole-Johan Dahl and Kristen Nygaard of the
Norwegian Computing Center in Oslo
• Smalltalk – introduced the term OOP
– Developed at Xerox PARC by Alan Kay and others in 1970s
PARC - Palo Alto Research Center Incorporated
Procedural versus OO Programming

Procedural Object-Oriented
procedure method
module object
procedural call message
variable attribute
Object-Oriented Programming Concepts
• Object
• Class
• Methods
• Message
• Properties
• Instance Variables
Class

OBJECT
- "An object is any thing, real or abstract, about which we
store data and those operations that manipulate the data.“

Definition: a thing that has identity, state, and behavior


– identity: a distinguished instance of a class
– state: collection of values for its variables
– behavior: capability to execute methods

• variables and methods are defined in a class


Example: String s1=new String(“Hello”);
Vehicle v=new Vehicle();
Class

CLASS
- A class is a set of objects that share common structure
and a common behavior.

A class is a
blueprint for
creating
many
similar
objects.
Example:
Dog class instances
Dog class (objects)
definition

Every Dog that is


created will have

Name
Age
Breed
Ginger Ramil Josua
6 2 3
Akita Beagle Affenpinscher
METHODS
- Methods are the
functions and code which
operate on the data and
express the behavior of
the object

- piece of code which is private String name;


called to perform the public void setName(String n){
activity requested by the name=n;
message }
public void getName(){
return name;
}
MESSAGE

- A message is the request you send to an object in order


to get it to do something: perform an action, return a value, etc.
PROPERTIES
- Properties are the data associated with the
object

Properties and methods in a class definition are


collectively called members
INSTANCE VARIABLES
- Or attributes are specific piece of data belonging
to an object
Object’s Data

methodOne

variableOne
methodTwo
INTERFACE
variableX

methodN
Instantiation

INSTANTIATION

• Object creation
• Memory is allocated for the object’s fields as defined in the
class
• Initialization is specified through a constructor
– a special method invoked when objects are created
import javax.swing.*; class JavaApp{
class Dog{ public static void main(String
Example:
private String name;
args[]) {
private String breed;
Dog(String n, String b){
//version1
name=n; Dog akita=new
breed=b; Dog("Josua","Akita");
} String name=akita.getName();
public void setName(String dogName){ String breed=akita.getBreed();
name=dogName;
} JOptionPane.showMessageDi
public String getName(){ alog(null, "Name:
return name;
"+name+"\n"+"Breed: "+breed);
}
public void setBreed(String dogBreed){
Dog beagle=new
breed=dogBreed; Dog("Ramil","Beagle");
}
public String getBreed(){ JOptionPane.showMessageDi
return breed; alog(null, "Name: "+
} beagle.getName()+"\n"+"Breed: "+
public boolean isBarking(boolean a){ beagle.getBreed());
return (a==true) ? true : false; }
}
}
}
import javax.swing.*; class JavaApp{
class Dog{ public static void main(String
Example:
private String name; args[]) {
private String breed;
//version2
Dog(){
} Dog akita=new Dog();
Dog(String n, String b){ akita.setName("Joshua");
name=n; akita.setBreed("Akita");
breed=b; } String name=akita.getName();
public String getName(){ String breed=akita.getBreed();
return name;
} JOptionPane.showMessageDia
public void setBreed(String dogBreed){ log(null, "Name: "+name+"\n"+
breed=dogBreed;
"Breed: "+breed);
}
public String getBreed(){ Dog beagle=new Dog();
return breed; beagle.setName("Ramil");
} beagle.setBreed("Beagle");
public boolean isBarking(boolean a){
return (a==true) ? true : false; JOptionPane.showMessageDia
} log(null, "Name: "+beagle.getName()+
public void setName(String dogName){ "\n"+"Breed: "+beagle.getBreed());
name=dogName; }
}
}
}
Fundamental Principles in OOP

ENCAPSULATION
Encapsulation

A. ENCAPSULATION

• A key OO concept: “Information Hiding”


• Key points
– The user of an object should have access only to those methods
(or data) that are essential
– Unnecessary implementation details should be hidden from the
user
– In Java/C++, use classes and access modifiers (public, private,
protected)
Encapsulation

Other Objects
Object Interfaces

Methods Data
ENCAPSULATION
- means that some or all of an object's internal
structure is "hidden" from the outside world.
public class EncapTest{
private String name; Sample Program
private String idNum;
private int age; import javax.swing.*;
public int getAge(){ public class RunEncap{
return age; public static void main(String args[]){
} EncapTest encap = new EncapTest();
public String getName(){ encap.setName("James");
return name; encap.setAge(20);
} encap.setIdNum("12343ms");
public String getIdNum(){
return idNum;
} JOptionPane.showMessageDialog(null,
public void setAge( int newAge){ "Name : " + encap.getName()+
age = newAge; " Age : "+ encap.getAge());
} }
public void setName(String newName){ }
name = newName;
}
public void setIdNum( String newId){
idNum = newId;
}
}
Sample Program

import java.awt.*;
import javax.swing.*;
class MyApp{
public static void main(String args[]) {
JFrame frame=new JFrame();
frame.setTitle("Encapsulation");
frame.setSize(400,300);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame,
frame.getTitle() +"\n"+ frame.getSize());
}
}
Output

import java.awt.*;
import javax.swing.*;
class MyApp{
public static void main(String args[]) {
JFrame frame=new JFrame();
frame.setTitle("Encapsulation");
frame.setSize(400,300);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame,
frame.getTitle() +"\n"+ frame.getSize());
}
}
Benefits of Encapsulation

• The fields of a class can be made read-only or write-only.


• A class can have total control over what is stored in its fields.
• The users of a class do not know how the class stores its data.
A class can change the data type of a field, and users of the
class do not need to change any of their code.
Polymorphism

B. POLYMORPHISM

- The ability of an object to assume many different


forms.
- Objects respond differently to the same
message.
- Also called overloading
class Addition{
Polymorphism
public int add(int n1, int n2){
return n1+n2;
}
public double add(double n1, double n2){
return n1+n2;
}
}
import javax.swing.*;
class JavaApp{
public static void main(String args[]){
int num1=Integer.parseInt(JOptionPane.showInputDialog(“Enter 1st #: ”));
int num2=Integer.parseInt(JOptionPane.showInputDialog(“Enter 2nd #: ”));
Addition a=new Addition();
int sum=a.add(num1,num2);
JOptionPane.showMessageDialog(null, sum);
}
}
class Addition{
Polymorphism
public int add(int n1, int n2){
return n1+n2;
}
public double add(double n1, double n2){
return n1+n2;
}
}

import javax.swing.*;
class JavaApp{
public static void main(String args[]){
double num1=Double.parseDouble(JOptionPane.showInputDialog(“Enter 1st #: ”));
double num2=Double.parseDouble(JOptionPane.showInputDialog(“Enter 2nd #: ”));
Addition a=new Addition();
double sum=a.add(num1,num2);
JOptionPane.showMessageDialog(null, sum);
}
}
Inheritance

C. INHERITANCE

- All behavior and properties of a class (parent) is passed


on to an object (instance) created from that class

- Allows you to create new objects(derived class) from


previously defined objects(base class)
What is inheritance?

Inheritance
- ability of objects in java to inherit properties and
methods from other objects

A
parent, superclass,
base class

child, subclass,
B
derive class
INHERITANCE

A "family tree" of all the classes in a program is called


a class hierarchy
INHERITANCE SCHEMES

No Inheritance

Single Inheritance

Multiple Inheritance

You might also like