Download presentation
Presentation is loading. Please wait.
Published byRaymond Randolph Gregory Modified over 8 years ago
1
5.1 Basics of defining and using classes
2
5.1.1 A review of class and object definitions A class is a template or blueprint for an object A class defines an object’s behavior & attributes An object knows something & how to do something An object stores information about what it knows When an object is created from a class it is called an instance of the class Objects are referenced by reference variables Object behavior is controlled by methods
3
5.2.1 Designing classes
4
5.2.2 Using classes Use the class keyword to define the class, along with attributes and methods Two steps to create an object: Declare a reference variable, then use the new operator to create an instance of the class Use the dot operator to access methods and attributes of the object
5
5.3.1 Attributes
6
5.3.2 Variables Instance variables hold data that is associated with each object, or instance of the class Class data is held in static variables. The class has only one copy of the class data, and it is available to all objects created from the class. To make a value immutable, use the final keyword.
7
5.4.2 Encapsulation & Access modifiers Encapsulation is the concept of hiding the internal details of an object Access modifiers are used to control access to object or class data
8
5.5.1 The constructor process Objects are constructed when the new operator is used An objects instance variables are initialized to default variables Method variables are not assigned default values
9
5.5.2 Constructors Constructor methods are called when the object is created If the class does not define a constructor, the compiler inserts one that does nothing If a class does include a constructor, the compiler does not insert one
10
5.6.1 Methods & method signatures An object invokes the method of another object to calling it using the method signature The method signature consists of the method name and the arguments and the return type The type of value returned by a method is declared in the method signature To declare that a method returns no value, use the void keyword
11
5.6.6 Concept of pass-by-value When a method is called, the method receives a copy of the argument Therefore the value can be altered by the method without affecting the original However, if the argument is a reference to an object, the method receives a copy of the reference This means that the method can access the object’s non-private data
12
5.6.7 Method types Instance methods are methods that are created when the object is created Class methods exist once the class is loaded, not as a part of an instance The static keyword is used to declare methods as class methods A special class method is the main method, which is used as an entry point for the JVM public static void main ( String args[ ] )
13
5.7.1 Using this in constructors and methods Every instance method has a variable called this this is a reference to the current object
14
5.8.2 Encapsulation & overloading Encapsulation hides the details of a class In general, instance variables should be private and methods should be public Hidden instance data can be altered and retrieved using getter and setter methods Overloading involves having several methods with the same name The JVM knows the correct method to use by the number and type of the arguments
15
5.10.1 Initializing data Instance variables are initialized with a default value based on the data type Instance variables can be explicitly initialized when they are declared Initialization blocks are blocks of statements that can initialize instance variables Initialization blocks can be static or non-static
16
5.10.2 Scope of variables Instance data is available as long as the object is in use or a reference to it exists Class data exists as soon as the class is loaded, and before any objects are created Method variables only exist while the method is in use, and only in the block where they were declared Arguments are available for the life of the method
17
5.10.6 Finalizers All objects have a finalizer method, that is called just before the JVM disposes of the object Any clean-up code can be included in the finalizer method Because the garbage collection is automatic, use of the finalizer method is often not necessary
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.