2. Old Programming Technique:
Structures
struct queue{
int a[5];
int head;
int tail;
};
struct stack{
int a[5];
int top;
}
void main()
{
struct queue Q;
struct stack S;
print( Q.a[2] );
print( S.a[2] );
add(S, 3);
add(Q, 2);
}
void add(queue Z, int x){
<codes for
adding queue
elements here>
}
void remove(queue Z, int x){
<codes for
removing queue
elements here>
}
void add(stack Z, int x){
<codes for
adding stack
elements here>
}
void remove(stack Z, int x){
<codes for
removing stack
elements here>
}
?
3. New Programming Technique:
Object-Oriented
void main()
{
queue Q;
stack S;
print( Q.a[2] );
print( S.a[2] );
S.add(3);
Q.add(2);
}
class queue{
int a[5];
int head;
int tail;
void add(int x){
<codes here>
}
void remove(int x){
<codes here>
}
};
class stack{
int a[5];
int top;
void add(int x){
<codes here>
}
void remove(int x){
<codes here>
}
}
4. Chapter 3 - Object-Orientation
What is OO Programming
A type of programming in which programmers define not only
the data type of a data structure, but also the types of
operations (functions) that can be applied to the data structure.
In this way, the data structure becomes an object that includes
both data and functions.
In addition, programmers can create relationships between one
object and another.
For example, objects can inherit characteristics from other
objects.
6. ďObject-orientation is a new technology based on objects and
classes. It presently represents the best methodological framework
for software designers to develop complex large scale systems.
ďOne of the principal advantages of object-oriented programming
techniques over procedural programming techniques is that they
enable programmers to create modules that do not need to be
changed when a new type of object is added.
ďA programmer can simply create a new object that inherits
many of its features from existing objects. This makes object-
oriented programs easier to modify.
Chapter 3 - Object-Orientation
What is OOP?
9. What is a Class?
In manufacturing, a blueprint is a description of a device
from which many physical devices are constructed
In software, a class is a description of an object
A class describes the data that each object includes
A class describes the behaviour that each object
exhibit
In Java, classes support three key features of OOP
encapsulation
inheritance
polymorphism
Chapter 3 - Object-Orientation
10. What is an Object?
An object is an instance of the class. Objects store data
and provides method for accessing and modifying this data
Chapter 3 - Object-Orientation
data/properties/fields â are the attributes of the object
methods â are functions that manipulate the data
Class:
Vehicle
Blue Print
Object: Actual Auto
Object: Actual Bus
Object: Actual Jeep
15. Chapter 3 - Object-Orientation
Constructors
â˘Constructors are useful for initializing objects before they
are being used.
â˘Parameters can be passed to the constructor in the
same way as for a method.
â˘Constructors are special purpose methods;
â˘a constructor is only used during instantiation to initialize
the object and is never used again.
â˘A constructor must follow the following rules:
1. A constructor's name must be the name of the class.
2. A constructor does not have any return type.
16. Chapter 3 - Object-Orientation
Constructors
Syntax:
<modifier> <classname>(parameters){
<statements>
}
Example:
String school = new String(âJAVA Universityâ);
String school = new String( );
17. Instantiating a Class
⢠Creating Objects:
<class name> <object name> = new <constructor call>;
String school = new String(âJAVA Universityâ);
18. Chapter 3 - Object-Orientation
Accessing Object Members:
The âdotâ notation <object>.<member>
This is used to access object members including
attributes and methods
Examples:
thing1.setX(47);
thing1.x=47; // valid only if x is public
21. JAVA PACKAGES
The standard Java classes are organized into packages.
Packages are a way of grouping related classes to avoid
potential naming conflicts.
The standard Java packages are
java.lang java.awt
java.applet java.awt.image
java.awt.peer java.io
java.net java.util
22. Chapter 3 - Object-Orientation
Package
Class1 Class 2 Class 3 Class n
Attributes
Methods
Attributes
Methods
Attributes
Methods
Attributes
Methods
24. Chapter 3 - Object-Orientation
java.lang package
--- contains the various classes that are essential to the
definition of the Java language or that, by their nature, need
to access the internals of Java in a way that most classes
cannot do.
Java.lang classes:
Boolean, Byte, Character, Character.Subset,
Character.UnicodeBlock, Class, ClassLoader
Compiler, Double, Float, InheritableThreadLocal
Integer, Long, Math, Number, Object, Package, Process
Runtime, RuntimePermission, SecurityManager, Short
String, StringBuffer, System, Thread, ThreadGroup
ThreadLocal, Throwable and Void
26. Chapter 3 - Object-Orientation
Java.lang
package
Class String
charAt(int index)
int compareTo(Object o)
int compareTo(String anotherString)
int compareToIgnoreCase(String str)
String concat(String str)
static String copyValueOf(char[] data)
static String copyValueOf(char[] data, int offset, int count)
boolean endsWith(String suffix)
boolean equals(Object anObject)
booleanequalsIgnoreCase(String anotherString)
byte[] getBytes()
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
27. Chapter 3 - Object-Orientation
Methods of Class String of the java.lang package
⢠public int length()
⢠public char charAt(int index)
⢠public String substring(int beginIndex, int endIndex)
To create an instance of String:
String proglang = âJAVAâ;
To use method length:
x = proglang.length();
30. Chapter 3 - Object-Orientation
Encapsulation
â˘Is the process of compartmentalizing the elements of an
abstraction that constitute its structure and behavior
â˘It is achieved through information hiding, which is the
process of hiding all the secrets of an object that do not
contribute to its essential characteristics; typically the structure
of an object is hidden, as well as the implementation of the
methods.
â˘Forces the user to use an interface to access data
â˘Makes the code more maintainable
31. Chapter 3 - Object-Orientation
Information Hiding
- is the process of hiding details of an object/function.
-is designing a method so that it can be used without
any need to understand the fine detail of the code.
- An object is composed of a public interface and a
private section that can be a combination of internal
data and methods. The internal data and methods are
the sections of the object hidden.
-The primary benefit is that these sections can change
without affecting other parts of the program
ABSTRACTION & ENCAPSULATION
BENEFITS OF ENCAPSULATION
32. Chapter 3 - Object-Orientation
ABSTRACTION & ENCAPSULATION
⢠Modularity. This means that an object can be maintained
independently of other objects. Because the source code for
the internal sections of an object is maintained separately from
the interface, you are free to make modifications with
confidence that your object won't cause problems to other
areas. This makes it easier to distribute objects throughout a
system.
BENEFITS OF ENCAPSULATION
33. Chapter 3 - Object-Orientation
Method and Member Visibility
Encapsulation hides a class internal details for the outside
world; if we were to provide getter and setter methods but
leave Account class member declaration as public
This is because public allows virtually any one to access
the member. By using private, we have effectively closed
all members to the outside world.
By doing this, we are enforcing encapsulation.
ABSTRACTION & ENCAPSULATION
34. Chapter 3 - Object-Orientation
Java provides the following visibility keywords which can be
used on members or methods:
public A method or member that is declared public is
accessible from any class.
protected A method or member that is declared protected
is accessible only to its subclass and the class itself.
private A private method or member of a class is only
accessible from within that class. Other classes including
subclass cannot access this member or method.
Keyword Self Subclass Others
Public yes yes yes
Protected yes yes no
Private yes no no
ABSTRACTION & ENCAPSULATION
35. Declaring Java
Classes:
public class Thing{
public int x;
Thing (); //we dnt nd
}
public class TestThing{
public static void main(String args[]{
Thing thing1 = new Thing();
thing1.x = 47;
System.out.println(âThing1 = â+
thing1.x);
}
}
Instantiating a
Class:
36. Declaring Java
Classes:
public class Thing{
private int x;
public int getter(){
return x;
}
public void setter(int newx){
x = newx;
}
}
Public class TestThing{
public static void main(String args[]{
Thing thing1 = new Thing();
thing1.x = 47; // invalid
thing1.setter(47);
System.out.println(âThing1 = â
+ thing1.getter();
}
}
Instantiating a
Class:
38. Chapter 3 - Object-Orientation
Abstraction
denotes the essential characteristics of an object that distinguish
it from all other kinds of objects focuses on the outside view of
the object
Kinds of Abstraction
Entity abstraction â an object that represents a useful model
of a problem-domain or solution domain.
Action Abstraction â an object that provides a generalized set
of operations, all of which perform the same kind of functions.
42. Declaring Java
Classes:
public class Thing{
public int x;
}
public class TestThing{
public static void main(String args[]{
Thing thing1 = new Thing();
thing1.x = 47;
System.out.println(âThing1 = â+ thing1.x);
}
}
43. Declaring Java
Classes:
public class Thing{
private int x;
public int getX(){
return x;
}
public void setX(int newx){
x = newx;
}
}
Public class TestThing{
public static void main(String args[]{
Thing thing1 = new Thing();
thing1.setX(47);
System.out.println(âThing1 = â
+ thing1.getX();
}
}
Instantiating a
Class:
44. Chapter 3 - Object-Orientation
Accessing Object Members:
The âdotâ notation <object>.<member>
This is used to access object members including
attributes and methods
Examples:
thing1.setX(47)
thing1.x=47; // valid only if x is public
46. Chapter 3 - Object-Orientation
public class Account3 {
private String name;
private int acctNo;
private float balance;
private boolean overdraft;
public Account3(String n, int no) {
name = n;
acctNo = no;
balance = 4000F;
overdraft = false;
}
public void deposit(float amt) { ---- }
public void withdrawal(float amt) { ---- }
public void transfer(Account from, float amt) { ---- }
Information Hiding
ABSTRACTION & ENCAPSULATION
47. Chapter 3 - Object-Orientation
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
public float getBalance(){
return balance;
}
public void setAccountNo(int ac){
acctNo = ac;
}
public int getAccountNo(){
return acctNo;
}
public void setOverdraft(boolean x)
{
overdraft = x;
}
public boolean getOverdraft(){
return overdraft;
}
}
ABSTRACTION & ENCAPSULATION
48. Chapter 3 - Object-Orientation
public class TestAccount3{
public static void main(String args[]){
Account3 myAccount = new Account3(âBin", 007);
// System.out.println("This account belongs to: " + myAccount.name);
System.out.println("Account of " + myAccount.getName());
// System.out.println("Current Balance is = "+ myAccount.balance);
System.out.println("Current Balance is ="+myAccount.getBalance());
System.out.println("After withdrawing P2000......");
myAccount.withdrawal(2000);
System.out.println("After withdrawing P100 ......");
myAccount.withdrawal(100);
// System.out.println("The new balance is = " + myAccount.balance);
System.out.println("The new balance is = " + myAccount.getBalance());
}
}
ABSTRACTION & ENCAPSULATION
49. Chapter 3 - Object-OrientationABSTRACTION & ENCAPSULATION
Open Account3.java and TestAccount3.java
Since TestAccount3.java has an object of type Account,
compile TestAccount3.java to compile the two files
EXERCISE: Exploring Classes and Objects
60. Chapter 3 - Object-Orientation
public class Account2 {
public String name;
public int acctNo;
public float balance;
public boolean overdraft;
public Account2(String n, int no) {
name = n;
acctNo = no;
balance = 4000F;
overdraft = false;
}
public void deposit(float amt)
public void withdrawal(float amt)
public void transfer(Account from, float amt)
}
61. Chapter 3 - Object-Orientation
public class TestAccount2 {
public static void main(String args[]){
Account2 myAccount = new Account2("Bin", 007);
// myAccount.name = "Bin"; myAccount.acctNo = 007;
// myAccount.balance = 4000; myAccount.overdraft = false;
System.out.println("This account belongs to: "+ myAccount.name);
System.out.println("Current Balance is = "+ myAccount.balance);
System.out.println("After withdrawing P2000......");
myAccount.withdrawal(2000);
System.out.println("After withdrawing P100 ......");
myAccount.withdrawal(100);
System.out.println("The new balance is = " + myAccount.balance);
}
}
62. Chapter 3 - Object-Orientation
public class Account3 {
public String name;
public int acctNo;
public float balance;
public boolean overdraft;
public Account3(String n, int no, float bal, boolean od) {
name = n;
acctNo = no;
balance = bal;
overdraft = od;
}
public void deposit(float amt)
public void withdrawal(float amt)
public void transfer(Account from, float amt)
}
63. Chapter 3 - Object-Orientation
public class TestAccount3 {
public static void main(String args[]){
Account3 myAccount = new Account3("Bin", 007, 5000,true);
// myAccount.name = "Bin"; myAccount.acctNo = 007;
// myAccount.balance = 5000; myAccount.overdraft = true;
System.out.println("This account belongs to: "+ myAccount.name);
System.out.println("Current Balance is = "+ myAccount.balance);
System.out.println("After withdrawing P2000......");
myAccount.withdrawal(2000);
System.out.println("After withdrawing P100 ......");
myAccount.withdrawal(100);
System.out.println("The new balance is = " + myAccount.balance);
}
}