COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

In order to understand object oriented programming, me must understand the ... A class is a blueprint from which individual objects are created. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 28
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 26-Jan-08
  • Lecture Set 6
  • Classes and Objects, Access Modifiers

2
Object Oriented Programming
  • In order to understand object oriented
    programming, me must understand the following
    programming terms / concepts
  • Objects
  • Classes
  • Inheritance
  • Interfaces
  • Packages

3
What is an object?
  • A software bundle of related state and behavior
  • State A value, usually stored in a variable (or
    field in some languages)
  • Behavior An action or function (a method in
    Java)
  • So, an object is a collection of similar or
    related variables and functions

4
Objects Real World Example
  • Consider a car
  • Behaviors (methods)
  • Brake
  • Accelerate
  • Change Gears
  • States (variables)
  • Speed
  • Gear
  • Direction

5
Why use objects?
  • Modularity
  • Source code for an object can be written and
    maintained independently of the source code for
    other objects
  • i.e., one file for a car, one file for a plane,
    etc
  • Code Re-use
  • Object can be reused in different programs
  • i.e., once your write the code for a car once,
    you are use it in as many programs as you would
    like and create as many car objects as you would
    like.

6
What is a class?
  • A class is a blueprint from which individual
    objects are created.
  • A class by itself is not a complete application
    (it does not contain a main function)
  • Example We could use a class called car to make
    many different car objects (classes create
    objects)

7
The car as a class
  • public class Car
  • int speed 0 //These are the instance
    variables of the class.
  • int gear 0 //They are assigned
    separately to each car
  • char direction n //object that is
    created.
  • //It should be noted that a
    single object is only
  • //aware of its own instance
    variables.
  • //These are the methods of the class which
    define what
  • //the object can do.
  • public void brake(int decrement)
  • speed speed - decrement
  • public void accelerate(int increment)
  • speed speed increment
  • public void changeDirection(char
    newDirection)
  • direction newDirection
  • public void changeGear(int newGear)

8
The car as a class
  • There is no main function in the class Car so
    how do we use it?
  • We must write a separate application that uses
    the car class

9
Using the car class in an application
  • public class CarDemo
  • public static void main(String args)
  • // Create two different Car objects
  • Car car1 new Car()
  • Car car2 new Car()
  • // Invoke methods on those objects
  • car1.accelerate(50)
  • car1.changeGear(5)
  • car1.printCarState()
  • car2.accelerate(15)
  • car2.changeGear(2)
  • car2.printStates()

10
Access Modifiers
  • Access modifiers are used to control access to
    members of a class
  • You can assign access modifiers to the class
    itself, to the instance variables (the fields) of
    the class, and to the methods of the class
  • As you will see in later lecture notes, it is
    important to assign a certain access modifier to
    your instance variables.

11
Public, Private, and Protected
  • When used in a class declaration Keywords used
    to specify the access levels for member variables
    and functions (methods).
  • public class example
  • When used on a method Used to specify the
    access levels for the method and its variables.
  • public static String printMe(String print)
  • When used on a variable Used to specify the
    access level for the single variable.
  • protected int a 100

12
Public, Private, and Protected
  • Public visible to all classes
  • Private Visible only to the class to which it
    belongs
  • Protected Visible to the class to which they
    belong and to all subclasses of that class.

13
Example
  • public class BankBalance
  • public String owner //Instance variables
  • public int balance //or fields.
  • public makeBalance( String name, int dollars )
  • owner name
  • //Check to be sure the balance is positive
  • if (dollars gt 0)
  • balance dollars
  • //If the balance is not positive, make it 0
  • else dollars 0

14
Example
  • In this example, both the string and integer
    instance variables are public.
  • Any object in the system can change the balance
    (even to a negative value).
  • This could cause the program to fail even though
    there are checks in the bank_balance class to
    prevent negative values.

15
Example
  • Consider the following code .
  • BankBalance b new BankBalance()
  • b.makeBalance(John Doe,50) //This is ok
  • b.makeBalance(Jane Doe, -50) //This would
    fail,
  • //balance would
    be 0
  • b.balance -50 //This is ok but should not be
  • //allowed.

16
Example modified
  • The better way to make this program would be to
    prove two methods, getBalance and setBalance.
  • Also the balance should be either private or
    protected.
  • Other objects can still access the data, but the
    cant input invalid data.

17
Example modified
  • public class BankBalance
  • public String owner
  • private int balance
  • public makeBalance( String name, int
    dollars )
  • owner name
  • if (dollars gt 0)
  • balance dollars
  • else dollars 0
  • public int getBalance()
  • return balance
  • public void setBalance(int dollars)
  • if (dollars gt 0)
  • balance dollars

18
The get and set methods
  • Declaring instance variables for with access
    modifier private is known as data hiding.
  • Often, we will need to allow the instance
    variables to be changed which can be done under
    the control of the Class.
  • Additionally, we may be to know the value of an
    instance variables --- which, again, will be done
    under the control of the Class.

19
Constructors
  • Constructors are used at the time of an objects
    creation to initialize an objects data (with the
    new keyword)
  • Constructors have no return type, but they do
    have a parameter list (or an empty parameter
    list.
  • A constructor with an empty parameter list is
    called a default constructor.
  • This is included (by default) by the compiler in
    any class that does not include its own
    constructor
  • It is NOT included if there is ANY constructor
    defined in the class. In other words, you would
    then have to create your own default constructor
    if you wanted to have one.

20
Constructors Example
  • public class BankBalance
  • public String owner
  • private int balance
  • public BankBalance(String name) //This is
    the constructor
  • owner name
  • balance 0
  • public int getBalance()
  • return balance
  • public void setBalance(int dollars)
  • if (dollars gt 0)

21
Import statements
  • Java treats files in the same directory as part
    of the same package
  • We will discuss packages later .
  • There is no need to import a class for use in an
    application if your files are in the same
    directory
  • as will be the case of our Assignment 3

22
The keyword this
  • In c a point to the current calling object
    (from inside a class function)
  • In Java a reference variable to the current
    calling object (from inside an instance method)
  • Can also be used to call one constructor for
    another in a class . (see next slide)

23
The keyword this calling one constuctor from
another
  • // constructor with 3 params
  • public Date(int m, int d, int y)
  • month m day d year y
  • // constructor with 2 params
  • public Date(int m, int d)
  • this(m,d,0) //calls constructor with 3
    params

24
The keyword this as a reference variable
  • public class Date
  • private int m, d, y
  • // constructor with 3 params
  • public Date(int m, int d, int y)
  • this.m m (Set this objects m instance
    variable)
  • this.d d (Set this objects d instance
    variable)
  • this.y y (Set this objects y instance
    variable)

25
Jar files
  • Used for aggregating many files into one
  • Created and extracted using the jar command
    (comes with the JDK)
  • Includes an optional manifest file
  • Determines how the JAR will be used
  • If the program in intended to be executed as a
    standalone program, the manifest file will
    specify which of the classes included the main
    method

26
Jar file instructions
  • See JavaJar.pdf

27
Stuff
  • Assignment 3 assigned.
  • Class next Tuesday is canceled.
  • Next Thursday
  • More on objects and classes in Java
  • Destroying, Java Docs, Garbage Collection, memory
    leaks, etc.
  • Java Docs will be part of the extra credit for
    assignment 3 . But the details will not be
    posted until later.
Write a Comment
User Comments (0)
About PowerShow.com