OOP Without Inheritance
OOP Without Inheritance
Structured Programming
• The programmer considers WHAT tasks the program must perform and codes the program focussing on the actions the
program DO
• Structured programming frequently employs a top-down design model, in which developers break down the overall program
structure into separate subsections/subprograms.
There are three major elements of structured programming:
• Sequence
• Alternation or decision
• Iteration or looping
Another key to structured programming is that
• Each block of code has a single entry point and a single exit point.
• In a sequence of statements, execution starts with the first statement, and terminates with the last statement.
• Also, this lets us break up long sequences of code into modules/subprograms.
Step wise refinement is used to break lengthy subprograms into smaller subprograms each with a clear identifiable task.
– The data structures used to achieve the tasks are chosen based on what the task is going to do and then the data needed to do
it.
– Almost any language can use structured programming techniques to avoid common pitfalls of unstructured languages.
Unstructured programming may result in poorly organized programs.
– Most modern procedural languages include features that encourage structured programming. Object-oriented programming
(OOP) uses structured programming techniques for program flow, and adds more structure for data to the model.
Event Driven Programming
GUIed programs typically are created using event-driven systems where all sorts of ''events'' can result in (processing being done).
This might include:
• Clicking on a button at the top of the screen
• Pulling down a menu
• Moving the mouse across an icon bar
The program does not have a definite start and end point. The sequence in which it is executed depends on what events the user
initiates.
• Every event driven program has a loop somewhere that catches received events and processes them.
• Notice that what we do with the events is of no interest to the main body, it simply collects the events and passes them to the
event handlers. This independence of event capture and processing is a key feature of event driven programming.
• The same program to sort an array could be written in an event driven program with three buttons with events to input an
array, sort the array and then display the array.
• It could also be written with one button to input, sort and display the array
Object Oriented Programming
• A type of programming in which programmers define not only the data type of a data structure, but also the types of
operations (methods/subprograms) that can be applied to the data structure.
• In this way, the data structure becomes an object that includes both data and methods.
• In addition, programmers can create relationships between one object and another. For example, objects can inherit
characteristics from other objects.
• One of the principal advantages of object-oriented programming techniques over procedural (structured) 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.
What is an Object?
• Just like a variable is defined by a type, an object is defined by a class (that is a programmer-defined type).
• A class contains
– type statements that define data variables / fields for the object and
– definitions of the methods that will act on these fields.
• Classes can also be thought of as blueprints for objects.
• A class from which objects are created cannot be run; it merely defines the object.
• An object is instantiated (created) using the class. An object is an instance of a class, because each object created from the
class can have different properties.
Declaring and Instantiating an Object – Java
An object is declared and instantiated with the code
ClassName obj = new ClassName();
obj addrx
The class will dictate what field variables exist, and the methods that will ac
addrx
fields
The variable obj stores a reference to the location of the object in memory.
methods
obj
addrx
fields
addrx
methods
Choose Fields
We would have to decide what field variables we would need for each Person object.
Each Person has a
name and a telephone number
According to good OO practices, these variables (fields) need to be private so that they are not accessible outside the class in
which they are defined. This is called information hiding and is used to improve the code reliability.
Accessor and Mutator Methods
• If the variables are private, how do we access or change (mutate) these variables?
• We create methods called accessor and mutator methods.
• An accessor method is a typed method (called function in Delphi) that returns a field’s value.
• A mutator method is a void method (called procedure in Delphi) that changes a field’s value.
Person Class
The class Person will be coded by:
• Defining the private fields name and telNo
• Coding a constructor method to create objects
• Coding accessor methods for each field
• Coding mutator methods for each field
• Coding a toString method to combine all the private fields into a String, so that the data in all the fields can be displayed
in an output statement.
Take in consideration:
• Recall that the Person class cannot be run
• It is a blueprint for future objects
• Objects of the Person class must be instantiated in another class or application
• An application is a class that can be run
• In Java, application classes contain a main method
Application class in Java
• The application class must contain code to instantiate a Person object
• We need to know what parameters the constructor expects
in Person class:
public Person (String n, String t){ The Person object is instantiated by sending a name actual parameter value
name = n; and a telephone number actual parameter value.
telnum = t;}
in Application class (class with public static void main)
Person ppl = new Person(“Fred”,“455-1234”);
In Memory
The variable ppl will now point to an object of the Person class with the data “Fred” and the phone number “455-1234”
addrX
The object referenced by ppl has its own field variables with access to the metho
Fred
ppl 455-1234
package myPerson;
public class Person {
//fields
private String name;
private String telnum;
//parameterised constructor receives values and use it to change all fields
public Person (String n, String t) {
name = n;
telnum = t;
}
//mutator method receive a parameter and use it to change value of a field
public void setName(String name) { this.name = name; }
public void setTelnum(String telnum) { this.telnum = telnum; }
//accessor method returns the value of a field
public String getName() { return name; }
public String getTelnum() { return telnum; }
//toString method that overwrites the default toString method
//returns a string that will be used to display information from this class
public String toString() {
return name +addSpaces (name, 15) +telnum;
}
//typed private helper-method used to make neat columns in the display
private String addSpaces(String n, int w) {
String out="";
for (int i=n.length();i<w;i++)
{ out+=" "; }
return out;
}
}
package myPerson;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
//Create an instance (object) of the class using the parameterised constructor
Person ppl = new Person("Fred","455-123");
//Display result of the toString method of the object
JOptionPane.showMessageDialog(null, ppl);
}
}