Java Reference Types: Object Oriented Programming ...

Java Reference Types: Object Oriented Programming Languages ? Object Oriented Programming (OOP) is based on the concepts of

? Classes, and ? Objects ? Anyone who has taken biology is familiar with this concept ? Bio is based on the following classification scheme:

Kingdom Phylum Class Order Family Genus Species ? Each classification describes characteristics of a group of living organisms ? Each subclass is more specialized than the superclass under which it is listed ? Each subclass inherits the characteristics of its superclass (and thus from all of its ancestors) ? Each class can be thought of as a template - a model - a description - of a set of organisms ? While classifications (classes from here on) are general, we can also talk about specific organisms ? Your pet Irish Setter Boru ? The third marigold in the planter on your back porch ? The wasp that stung you this morning ? We call specific instances of a class objects ? Objects that represent a given class (e.g., Dogs ) have a set of common characteristics specified by their class ? Yet each object is unique, even if the values of their characteristics are the same

1

Java Reference Types: Object Oriented (2) Programming Languages ? Java is an OO language

? The concept of class is intrinsic to Java programming ? The following provides an overview of OO terminology as it applies to OOP

This is not intended as a lesson in how to program with objects - that comes at the end of the semester

? However, since everything in Java is ultimately based on classes and objects, a general understanding of the main concepts helps in understanding some of the programming aspects we will see prior to then

2

Java Reference Types: Classes

? In Java, a class represents a template that describes a set of objects we want to represent in a program

? Unlike the biology example, a class can represent anything we want Bank accounts Books Shapes Coordinates Cars etc.

? Group of objects with similar properties

? Class describes a set of objects

? Class consists of characteristics and behaviors

? Characteristics describe the class ? Behaviors represent the actions that a class can perform (or have performed on

it) ? In general, these are called class members

? We call characteristics data members

? Represented by variables called instance variables

? We call behaviors Methods function members

? Represented by methods

? For example, consider a bank account

? Characteristics of bank accounts might be an account number a balance a type (e.g., checking, savings) ...

? Behaviors might be: making a withdrawal making a deposit calculating interest ...

3

Java Reference Types: Classes (2)

? The characteristics would be represented as instance variables such as

int accountNum;

double balance;

int type;

//0 for checking, 1 for savings

? The behaviors would be represented by methods such as

public void withdraw (double amount); public void deposit (double amount); public double getBalance (); public double calcInt ();

? Class names in Java start with capital letters by convention: Scanner, System, String

4

Java Reference Types: Objects ? Objects represent specific instances of a class

? For example, a bank account object might have accountNum = 55729 balance = 7695.32 type = 0

? An object is represented by the values of its instance variables, which collectively is referred to as the object's state

? Note that every object has ? Its own set of instance variables, and ? Its own set of behaviors

? This means that - for the bank account object above - its deposit() method will only deposit amounts into that particular object and will only change the value of that particular object's balance variable

? Objects are created by invoking a method known as a constructor ? Every Java class has such a method ? The constructor is invoked (and a new object is created) using the call new ? Syntax:

For example: Scanner keyboard;

keyboard = new Scanner (System.in);

The argument list may be empty and depends on the particular class Note that many classes have multiple constructors that differ in the number

and/or type of arguments

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download