Object Oriented Concepts
Object Oriented Concepts
Objectives
After this chapter, you should be able to:
Determine the differences between procedural and Object –
Oriented programming.
Learn about the 3 main principles of Object – Oriented
programming.
◼ Encapsulation
◼ Inheritance
◼ Polymorphism
Know more about classes and objects.
Procedural vs. Object-Oriented
Programming
The unit in procedural programming is function, and unit in
object-oriented programming is class
Procedural programming concentrates on creating functions,
while object-oriented programming starts from isolating the
classes, and then look for the methods inside them.
Procedural programming separates the data of the program from
the operations that manipulate the data, while object-oriented
programming focus on both of them
struct Account {
char *name;
int accountNum;
float balance;
char accountType;
};
Procedural Approach cont’d
Focus is on procedures
All data is shared: no protection
More difficult to modify
Hard to manage complexity
Procedural vs. Object-Oriented
Procedural Object Oriented
Data 1 Data 1
Data 2 Proc 1
Data 3
Data 2
PROCEDURE
Proc 2
Proc 1
Proc 2 Data 3
Proc 3 Proc 3
Procedural Style vs OOP Style
Procedural Style
• A client is responsible for invoking appropriate supplier
functions to accomplish a task.
OOP Style
• Suppliers are responsible for conforming to the standard
interface required for exporting the desired functionality to a
client.
Object Oriented Programming
Data and operations are grouped together
Account Interface:
Animal
Mammal Reptile
Account
Deposit()
Balance:
Saving Current
Account Account
Deposit() Deposit()
Balance: Balance:
27
Constructor
For proper initialization, a class must provide a
constructor.
A constructor is called automatically when an object
is created:
◼ It is usually declared public.
◼ It has the same name as the class.
◼ It must not specify a return type.
The compiler supplies a no-arg constructor if and
only if a constructor is not explicitly provided.
◼ If any constructor is explicitly provided, then the compiler
does not generate the no-arg constructor.
Is a method that is called when a new object is
created
Can perform any action you write its definition
Defining & overloading Constructors
public class Movie{
private String title;
private String rating = “PG”;
public Movie(){
title = “One More Chance”;
}
public Movie(String newTitle){
title = newTitle;
}
}
----------
Movie mov1=new Movie();
Movie mov1=new Movie(“2 fast 2 furious”);
The this Method
When defining a constructor, another common
action is to call one of the other constructors
in the same class.
Sharing Code between Constructors
Movie mov1 = new Movie();
Movie mov2 = new Movie(“A”);
-----
public class Movie{
private String title, rating;
public Movie(){
this(“G”);
}
public Movie(String newRating){
rating = newRating;
}
}
Sharing Code between Constructors
class Weight {
int lb, oz;
public Weight (int a, int b) { lb = a; oz = b;}
public Weight (int x) { this( x, 0); }
}
How to extend classes?
Inheritance: mechanism for extending behavior of
classes; leads to construction of hierarchy of classes
What happens when class C extends class D:
◼ Inherits instance variables
◼ Inherits static variables
◼ Inherits instance methods
◼ Inherits static methods
◼ C can:
Add new instance variables
Add new methods (static and dynamic)
Modify methods (only implementation)
Cannot delete anything
Inheritance
---Parent class
class Person{
Person(){
System.out.println(“James”);
}
}
---Sub class
public class Driver extends Person{
Driver(){
System.out.println(“Bond”);
}
public static void main(String args []){
Driver d = new Driver();
}
}
Super method
---Parent class
class Person{
private String name;
Person(){
this(“Juan”);
}
Person(String n){
name=n;
}
}
---Sub class
public class Driver extends Person{
Driver(){
super();
}
}
Encapsulation
Applying Encapsulation in Java
Instance variables must be declared as private.
Only instance methods can access private instance
variables.
private decouples the interface of the class from its
internal operation.
components }
Java GUI
Action Listener: tells the program to listen to a certain event
A simpler solution
◼ Use NetBeans (or other IDEs) to build GUIs
Java GUI
Java GUI
Java GUI
Java GUI
Java GUI