Programming a computer is one of the most difficult tasks you will undertake. It will require creativity, intelligence, logic, and the ability to build and use abstractions. Object-oriented programming is sometimes referred to as a new programming paradigm. The word paradigm means a set of theories, standards, and methods that together represent a way of organizing knowledge; that is, a way of viewing the world. In trying to understand what is meant by the term object-oriented programming we will examine a few real-world situations and then look at how we could have the computer model the problem-solving techniques employed.
A useful way to begin an object-oriented design is to write down a description of the problem. The nouns give us an idea what classes and data we might need and the verbs give us an idea of the member functions. This will not always be a complete correspondence but will give you a good start.
Step 2 in creating an object:
Two relationships are important to the second stage of object-oriented design. These two relationships are known as the is-a relationship and the has-a (or part-of) relationship.
is-a - holds between two concepts when the first is a specialized instance of the second. The is-a relationship defines class-subclass hierarchies.
example:
example:
Introduction to Objects
The information we manage in a Java program is either represented as primitive data or as objects.
Our first example of a simple object in java: the Button class.
// Button.java: a class representing a button object
public class Button{
// the state of the button
// myState == true means the button is pushed
// myState == false means the button is cleared (not pushed)
private boolean myState;
// constructor -- used when a new button is created (starts cleared)
public Button(){
myState = false;
}
// accessor -- used to fetch the button's state
public boolean getState(){
return myState;
}
// used to print the button's state
public void tellState(){
System.out.println(" From tellState: I am in state " + myState);
}
// used to automatically print the button's state
public String toString(){
return " From toString: I am in state " + myState;
}
// modifier -- used to "push" the button
public void pushButton(){
myState = true;
}
// modifier -- used to "clear" the button
public void clearButton(){
myState = false;
}
}
// ButtonDemo.java: Test and demonstrate button objects
public class ButtonDemo {
public static void main(String args[]) {
boolean thisButtonState;
Button thisButton = new Button();
// we can create any number of separate buttons
Button anotherButton = new Button();
// first work with thisButton
System.out.println("After Constructing thisButton");
thisButton.tellState();
System.out.println("Push Button");
thisButton.pushButton();
thisButton.tellState();
System.out.println("Clear Button");
thisButton.clearButton();
thisButton.tellState();
System.out.println("Call to GetState");
thisButtonState = thisButton.getState();
System.out.println("Current State is: " + thisButtonState);
thisButton.pushButton();
System.out.println("Call to toString ");
System.out.println( thisButton );
// now try out the other button: anotherButton
System.out.println("After constructing anotherButton");
anotherButton.tellState();
// continue as before
}
}
/* here is the output from a run:
ten42% java ButtonDemo
After Constructing thisButton
From tellState: I am in state false
Push Button
From tellState: I am in state true
Clear Button
From tellState: I am in state false
Call to GetState
Current State is: false
Call to toString
From toString: I am in state true
After constructing anotherButton
From tellState: I am in state false
*/
The print and println methods:
System.out.println("Push Button"); outputs Push Button and moves to the next line