Professional Documents
Culture Documents
CONCEPTS
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.“
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
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
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
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
B. POLYMORPHISM
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
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
No Inheritance
Single Inheritance
Multiple Inheritance