Introduction to ClassesObjects - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Introduction to ClassesObjects

Description:

A Blueprint to tell the Java Virtual Machine how to create an object of that type ... Tester Class. Application Class. Within other / 2nd class ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 45
Provided by: debrac7
Category:

less

Transcript and Presenter's Notes

Title: Introduction to ClassesObjects


1
Introduction to Classes/Objects
  • CIS 120

2
Object Oriented Programming (OOP)
  • Combines the power of procedural programming with
    Objects
  • More Flexibility
  • Modularity
  • Clarity
  • Reusability
  • Through the use of Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

3
Objects
  • Focus is on the Object
  • Unit of Programming is the Class
  • A class is NOT an Object
  • It is used to create them
  • A Blueprint to tell the Java Virtual Machine how
    to create an object of that type
  • Abstract
  • Objects of the class are Instantiated (created)
  • Concrete, One specific instance of a class

4
Classes
  • Programmers create their own reference types
    Classes
  • Each Class Contains
  • Attributes / Variables Things the object knows
  • Represents the state of the object
  • Each object will have their own unique values for
    the attrbutes
  • Methods Things the object can do
  • Operations on the data
  • Invoking a method is asking an object of the
    class to perform the task

5
Encapsulation
  • All of the attributes and methods that are needed
    by that class are included as part of the class
    definition
  • Details of how the class performs its tasks are
    kept hidden from the user of the methods
  • Client can use the method without knowing how it
    works
  • Programmer can change how the method works
    without affecting anyone using the method (as
    long as the method header doesnt change)

6
Inheritance
  • Creating classes from other pre-existing classes
  • Every class in Java inherits from another class
    (the Object class)
  • Child objects gain the existing attributes and
    behaviors of the Parent class
  • Can Modify them
  • Can Add to them

7
Class Definitions
  • Variables
  • Class Variables
  • Instance Variables
  • Constructors
  • Methods
  • Class Methods
  • Instance Methods

8
Variables
  • Class Variables
  • Defined with keyword static
  • Variables where the entire class has one copy of
    this variable
  • Defined by the class one copy of these for the
    entire class, not one per object of the class
  • Example a counter (more on this later)

9
Variables
  • Instance Variables
  • Variables / Attributes that all objects of the
    class have their own copy of
  • Each instance / object of the class will have
    unique data values for the Instance Variables

10
Constuctors
  • Special Methods used to create an object of the
    class
  • Use Keyword new to call the constructor
  • Constructors have the same name as the class
    (even start with a capital letter)
  • Constructors have NO Return Type not even VOID
  • Default Constructor no formal parameters
  • Sets default values for variables
  • Overloaded Constructor different parameter lists

11
Methods
  • Class Methods
  • Defined with keyword static
  • Methods defined by the class that are available
    to be called but are not performed by any one
    specific object of the class
  • Performs some type of computation or useful task
  • Not performed by a specific object

12
Methods
  • Instance Methods
  • Methods performed by a specific object of the
    class
  • Object can be thought of as an actor performing
    the method

13
Creating a Class
  • Class Header
  • What class headers have we created?
  • What has it contained?
  • Access Control Keyword
  • Public, Private, Protected, ltNonegt
  • Keyword class
  • Name of the class (capitalized)
  • Class definition contained in

14
Create a Circle Class
  • public class Circle
  • // all parts of class definition go here
  • // end class circle

15
Variables
  • What data does a circle contain?
  • Radius
  • Should Radius be an Instance Variable or Class
    Variable

16
Create the Radius Variable
  • Created the same as any other variable
  • Data type name
  • Can include Access Control Keywords to limit
    access (Public, Private, Protected, ltNonegt)
  • // instance variables
  • public double radius
  • // end class

17
Constructor
  • Default Constructor
  • You should always have a default constructor
  • No Formal Parameters
  • Public Circle ( )
  • radius 0.0
  • // end default constructor

18
Method Overloading
  • Additional Constructor(s) ? Different Formal
    Parameter Lists
  • For Circle, used when there is a Radius
  • Public Circle (double newRadius)
  • radius newRadius
  • // end double constructor

19
Methods
  • Instance Methods
  • getArea ? an object of class Circle should be
    able to determine its radius
  • Instance Method needs a specific instance of a
    circle, with a set radius, to call the method
  • public double getArea ()
  • return radius radius Math.PI
  • // end getArea method

20
(No Transcript)
21
Creating an Object of the Class
  • Not done within the Class Definition
  • Created within a separate class
  • Tester Class
  • Application Class
  • Within other / 2nd class
  • Invoke a constructor using the new keyword
  • objectReference new ClassName (actual
    parameters)

22
Object Reference
  • Reference to the instance of the object in memory
  • Like a variable name only for an instance of an
    object
  • Object Reference is created on the data stack
  • Points to location in Heap memory where object is
    actually created
  • Create like declaring a variable
  • must at some point call constructor
  • Circle myCircle
  • myCircle new Circle ()
  • Circle myCircle new Circle () // same as 2
    lines above

23
(No Transcript)
24
Accessing Data and Methods
  • After an object has been created
  • Data and methods can be accessed using the .
    (dot) operator
  • ObjectReference.dataField
  • myCircle.radius
  • ObjectReference.method(arguments)
  • myCircle.getArea()

25
(No Transcript)
26
Static Variables / Methods
  • Static Variables
  • All instances of the class share the same data
  • Data values are stored in a common memory
    location
  • All objects of the class are affected if one
    object changes the value of a static variable
  • Example count
  • Keep track of how many circle objects have been
    created

27
(No Transcript)
28
Static Method
  • Performed by the class not one instance/object
    of the class
  • Example getNumberOfCircles
  • Not specific to any one instance
  • Call with ClassName.methodName

29
(No Transcript)
30
Variables vs. Reference Objects
  • Variables represent a memory location on data
    stack with a value
  • Tell compiler what type of data the variable can
    hold
  • Variable of a Reference Type (object reference)
    holds an address to a memory location on the heap
    to where the contents of the object are held

31
Variable vs. Reference Object
  • Copying a Variable
  • Primitive type assignment i j
  • Before After
  • i i
  • j j

1
2
2
2
32
Variable vs. Reference Object
  • Copying a Reference to an Object
  • Object Type Assignment c1 c2
  • Before

X
33
Access Control Keywords
  • Visibility Modifiers
  • Control Access to Variables, Methods Classes
  • Public makes classes, methods and variables
    accessible from any class, sub-class, package
  • Protected makes classes, methods and variables
    accessible from the same class, sub-classes, and
    package (but not other classes)
  • ltNonegt makes methods and variables accessible
    only from the same class and package (not
    sub-class or other classes)
  • Private makes methods and variables accessible
    only from within its own class (not sub-classes,
    package, or other classes)

34
Why use Access Control Keywords?
  • In class Circle
  • Radius and numberOfCircles are public
  • Can be modified directly by ANY user
  • This is a BAD idea
  • Data may be tampered with
  • User might set numberOfCirlces to any number
  • Makes the class more difficult to maintain and
    vulnerable to bugs
  • Changes to radius would then require a change to
    the class and a change to all other classes that
    use radius

35
Private
  • Prevent direct modifications of Class Variables
  • Make them private
  • Data Field Encapsulation
  • Make the user call a method to access the data in
    the variable
  • Private Variables cannot be directly accessed
    outside of the class that defines the private
    field
  • Use Accessor and Mutator methods for private data
    fields

36
Accessor Methods
  • A get method
  • Used to get the value of a private class
    variable
  • public returnType getVariableName()
  • return variableName
  • // end get method

37
Mutator Methods
  • A set method
  • Used to set a new value/update for a private
    class variable
  • Public void setVariableName (dataType
    valueVariable)
  • variableName valueVariable
  • // end set method

38
Circle Code of Accessor/Mutator
39
Change Testing Class
Method Call
40
Passing Objects to Methods
  • Objects can be passed to methods as parameters
  • Passing an object is actually passing the
    reference to the object
  • The memory location of the object on the heap
  • A new method object is created, however, it the
    reference is to the same memory location as the
    original object
  • 2 References to the Same Memory Location
  • Changes made to the object in the method WILL
    affect the object in the calling method

41
Scope of Variables
  • Local Variables Declared and Used inside a
    method
  • Same variable names can be reused in different
    methods (not a good idea)
  • Instance and Class Variables
  • Scope is the entire class regardless of where
    declared
  • Can be declared in any order in the class
  • A class variable can be declared only once
  • If a class variable has the same name as a local
    variable (really not a good idea) the local
    variable takes precedence

42
this keyword
  • this.variableName
  • this serves as a proxy for the object that
    called the method
  • Stands for Refers to the calling object
  • It is like a blank waiting to be filled in by the
    object that invokes the method
  • Often implicit
  • Often omitted

43
.toString Method
  • Inherited from Object Class
  • All classes are child classes from Object
  • Intended to output object as a string
  • Actually outputs Object Reference
  • Hexadecimal Memory Address
  • Must be overloaded for each class to allow output
    of class objects

44
Circle.toString method
  • Instance Method in Circle Class
  • Call in Test Class
Write a Comment
User Comments (0)
About PowerShow.com