Professional Documents
Culture Documents
• Encapsulation
• Polymorphism
• Inheritance
OOP
Example
A car could be an object in Java.
Objects have attributes and methods.
Statements like
class Person01
{
String name;
int yearBorn;
int education;
}
Class Declaration Location
instantiation of an object
construction of an object
Person02()
{
System.out.println("Calling Default Constructor");
name = "John Doe";
yearBorn = 1980;
education = 0;
}
}
Constructor Notes
A constructor is a method with the same
identifier as the class.
class Person03
{
String name;
int yearBorn;
int education;
Person03()
{
System.out.println("Calling Default Constructor");
name = "John Doe"; yearBorn = 1980; education = 0;
}
}
// Java1404.java
// Stage-4 of the Person class.
// In this program direct access to Person data is denied by declaring all class data private.
// This program will not compile.
public class Java1404
{
public static void main(String args[])
{
System.out.println("Person Class, Stage 4\n");
Person04 p = new Person04();
System.out.println("Name: " + p.name);
System.out.println("Year Born: " + p.yearBorn);
System.out.println("Education: " + p.education);
System.out.println();
}
}
class Person04
{
private String name;
private int yearBorn;
private int education;
public Person04()
{
System.out.println("Calling Default Constructor");
name = "John Doe"; yearBorn = 1980; education = 0;
}
}
// Java1405.java
// Stage-5 of the Person class.
// The private data of the Person class is now properly accessed with
// public member "get methods" of the Person class.
public class Java1405
{
public static void main(String args[])
{
System.out.println("Person Class, Stage 5\n");
Person05 p = new Person05();
System.out.println("Name: " + p.getName());
System.out.println("Year Born: " + p.getYearBorn());
System.out.println("Education: " + p.getEducation());
}
}
class Person05
{
private String name;
private int yearBorn;
private int education;
public Person05()
{
System.out.println("Calling Default Constructor");
name = "John Doe"; yearBorn = 1980; education = 0;
}
public int getYearBorn() { return yearBorn; }
public String getName() { return name; }
public int getEducation() { return education; }
}
Private and Public Class Members
The principle of encapsulation requires that object data
members are only accessed by methods of the same object.
p.showData();
}
}
// Person08.java This file supports the Java1408.java executable file.
public class Person08
{
///// PRIVATE PERSON ATTRIBUTES ////////////////////////////////////////////////////
private String name;
private int yearBorn;
private int education;
///// CONSTRUCTORS ////////////////////////////////////////////////////////////////
public Person08()
{
System.out.println("Calling Default Constructor");
name = "John Doe"; yearBorn = 1980; education = 0;
}
public Person10(Person10 p)
{
System.out.println("Calling Copy Constructor");
name = p.name;
yearBorn = p.yearBorn;
education = p.education;
}
Copy Constructor
public Person10(Person10 p)
{
System.out.println("Calling Copy Constructor");
name = p.name;
yearBorn = p.yearBorn;
education = p.education;
}
A copy constructor is a constructor that instantiates
a new object as a copy of an existing object.
class Person
{
private String name;
public Person(String n)
{
name = n;
}
class Person
{
private String name;
public Person()
{
name = "John Doe";
}
}
What Is Going On? Part 1
p1 p2
@18d107f @3b0eb0
18d107f 3b0eb0
Kathy Tom
What Is Going On? Part 2
p2 = p1;
p1 p2
@18d107f @18d107f
18d107f 3b0eb0
Kathy Tom
What Is Going On? Part 3
p2.setName("George");
p1 p2
@18d107f @18d107f
18d107f 3b0eb0
George Tom
Objects and Simple Data Types
Store Information Differently
The same thing actually happens in the case of an object; however, with
an object the value is the memory address of the object itself. If this is
changed, it does not affect the memory address of the original object.
The only way changes made in a method remain after the method is if
the changes are made to the attributes of an object.
NOTE: Strings are objects, even though they tend to behave like
simple/primitive data types in some programs.
The true nature of Strings will be discussed in great detail in Chapter 16.
// Java1416.java
// This program intentionally uses identical parameter identifiers in the constructor method
// as the private attribute identifiers of the Car class. This has an undesirable result.
public class Java1416
{
public static void main (String args[])
{
System.out.println("\nJava1416.java\n");
Car ford = new Car("Explorer",2002,30000.0);
ford.showData();
System.out.println();
}
}
class Car
{
private String make;
private int year;
private double cost;
public Car(String make, int year, double cost)
{
make = make;
year = year;
cost = cost;
}
public void showData()
{
System.out.println("make: " + make);
System.out.println("year: " + year);
System.out.println("cost: " + cost);
}
}
// Java1417.java
// The problem of the previous program is solved by using the "this" reference
// to explicitly indicate the meaning of an identifier.
public class Java1417
{
public static void main (String args[])
{
System.out.println("\nJava1417.java\n");
Car ford = new Car("Explorer",2002,30000.0);
ford.showData();
System.out.println();
}
}
class Car
{
private String make;
private int year;
private double cost;
public Car(String make, int year, double cost)
{
this.make = make;
this.year = year;
this.cost = cost;
}
public void showData()
{
System.out.println("make: " + make);
System.out.println("year: " + year);
System.out.println("cost: " + cost);
}
}
// Java1418.java
// This program introduces the Engine class, which will be used in the next
// program to demonstrate composition.
public class Java1418
{
public static void main(String args[])
{
System.out.println("\nJava1418.java\n");
Engine iForce = new Engine(8,4.7,265);
System.out.println("Cylinders: " + iForce.getCylinders());
System.out.println("Size: " + iForce.getSize());
System.out.println("HorsePower: " + iForce.getHP());
System.out.println();
}
}
class Engine
{
private int cylinders; // number of cylinders in the engine
private double liters; // displacement of the engine in liters
private int horsePower; // horsepower of the engine
public Engine(int cyl, double size, int hp)
{
cylinders = cyl; liters = size; horsePower = hp;
}
public int getCylinders() { return cylinders; }
public double getSize() { return liters; }
public int getHP() { return horsePower; }
}
// Java1419.java
// This program demonstrates "composition", which uses multiple classes in
// such a way that an object of one class become a data member of another class.
public class Java1419
{
public static void main(String args[])
{
System.out.println("\nJava1419.java\n");
Car toyota = new Car("Tundra",4,"Red",8,4.7,265);
toyota.displayCar();
}
}
class Engine // shown on previous slide
class Car
{
private String carMake;
private int carDoors;
private String carColour;
private Engine carEngine;
public Car(String cm, int cd, String cc, int cyl, double size, int hp)
{
carMake = cm; carDoors = cd; carColour = cc;
carEngine = new Engine(cyl,size,hp);
System.out.println("Car Object Finished\n");
}
public void displayCar()
{
System.out.println("carMake: " + carMake);
System.out.println("carDoors: " + carDoors);
System.out.println("carColour: " + carColour);
System.out.println("cylinders: " + carEngine.getCylinders());
System.out.println("size: " + carEngine.getSize());
System.out.println("horsePower: " + carEngine.getHP());
}
}
// Java1420.java
// This program demonstrates the use of static "class"
// methods that do not require the creation of an object.
class Calc
{
public static double add(double a, double b) { return a + b; }
public static double sub(double a, double b) { return a - b; }
public static double mul(double a, double b) { return a * b; }
public static double div(double a, double b) { return a / b; }
}
// Java1421.java
// This program uses the same Calc class as the previous program. This time an object
// of the Calc class is constructed. This is possible, but not necessary.
public class Java1421
{
public static void main(String[] args)
{
System.out.println("\nJava1421.java\n");
Calc c = new Calc();
System.out.println("100 + 33 = " + c.add(100,33));
System.out.println("100 - 33 = " + c.sub(100,33));
System.out.println("100 * 33 = " + c.mul(100,33));
System.out.println("100 / 33 = " + c.div(100,33));
System.out.println();
}
}
class Calc
{
public static double add(double a, double b) { return a + b; }
public static double sub(double a, double b) { return a - b; }
public static double mul(double a, double b) { return a * b; }
public static double div(double a, double b) { return a / b; }
}
Class Methods
It is possible to access methods directly without first creating
an object. Methods that are accessed in this manner are
called Class Methods.
System.out.println(Calc.add(X,Y));
It is possible to access static methods with object identifiers.
This is considered poor styling and should be avoided.
// Java1422.java
// This program is an example of multiple objects using the same static class attribute. Note
// how the use of object identifiers with object methods and the class identifiers with class methods.
public class Java1422
{
public static void main(String[] args)
{
System.out.println("\nJava1422.java\n");
int newScore;
Game.setHS(1000);
System.out.println("Starting with default high score of 1000\n");
Game p1 = new Game("Tom");
System.out.println(p1.getName() + "'s score is " + p1.getScore() +
" compared to " + Game.getHS() + " high score\n");
newScore = p1.getScore();
if (newScore > Game.getHS()) Game.setHS(newScore);
Game p2 = new Game("Sue");
System.out.println(p2.getName() + "'s score is " + p2.getScore() +
" compared to " + Game.getHS() + " high score\n");
newScore = p2.getScore();
if (newScore > Game.getHS()) Game.setHS(newScore);
Game p3 = new Game("Lois");
System.out.println(p3.getName() + "'s score is " + p3.getScore() +
" compared to " + Game.getHS() + " high score\n");
newScore = p3.getScore();
if (newScore > Game.getHS()) Game.setHS(newScore);
System.out.println("Highest score is " + Game.getHS());
System.out.println();
}
}
// This class is used with program Java1422.java from the previous slide.
class Game
{
private static int highScore; // highest score of any game played
private int score; // current object score
private String player; // name of the current player
class Budget
{
private static int schoolBalance; // money left in school budget
private int deptBalance; // money left in department budget
private String deptName; // stored department name
public void drawFish(Graphics g, int row, int col, int clr, int num)
// draws a fish at the specified [row][col] grid location with color (clr) and a number(num)
// to identify the fish