Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:

Similar presentations


Presentation on theme: "1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:"— Presentation transcript:

1 1 Chapter 5: Defining Classes

2 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance: each instance (object) of the class will have its own copy of instance variables –Class: Only one copy is shared by all objects Types of methods: Instance & class (static) method Accessing variables and methods (dot operator) Accessing static variables and methods? Methods: name, return value, & parameters Acessing class Data members in methods Example: p. 181 Sphere.java

3 3 Basics of Classes (cont) Initialization blocks are used to initialize large number of variables like the element of a large array. Page 184 (try non static) Constructor: Mainly for guaranteeing that class variables are properly initialized. P. 186: Sphere & CreateSphere (Compile & run) Method overloading and multiple constructors (195-Sphere) Using objects (197-Point, 199-Line, & 201- TryGeometry)

4 4 JAVA Packages Recursion means a method can call itself!! A behavior that can be very useful. Ex: 203-PowerCalc.java A Java package is basically a group of Java classes stored in a directory that has the exact name as the package name To create a package use the “package ” as the first statement in all the classes of that package To compile a class in a package, the path to that package should be defined in your “classpath” var. To run a class in a package, you need to run JVM from the upper directory containing that package and to specify the name of the package & the class name separates by a period. Standard Java packages. See documentation

5 5 Access Attributes & Nested Classes There are four attributes: friendly, public, private, and protected Ex: 217-Point, 218-Line, & 219-TryPackage Java nested classes & static nested classes. Ex: 224-MagicHat & 225-TryNesstedClasses Using Non-static nested classes. Ex: 227- MagicHat The finalize() method You can declare native methods using the “native” keyword but you lose portability.


Download ppt "1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:"

Similar presentations


Ads by Google