Docsity

Java Programming: Understanding Classes, Variables, and Methods, Slides of Object Oriented Programming

Partial preview of the text

Download Java Programming: Understanding Classes, Variables, and Methods and more Slides Object Oriented Programming in PDF only on Docsity!

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: 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: return type, name, & parameters
  • Accessing class Data members in methods
  • Example: Sphere.java

4

JAVA Packages

  • Recursion means a method can call itself!! A behavior that can be very useful. Ex: 234-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” variable or use – classpath compile option
  • 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

Access Attributes & Nested Classes

  • There are four attributes: friendly, public,

private, and protected

  • Ex: 251-Point, 252-Line, & 254-TryPackage
  • Java nested classes & static nested classes.

Ex: MagicHat & TryNesstedClasses

  • Using Non-static nested classes. Ex: MagicHat
  • The finalize() method
  • You can declare native methods using the

“native” keyword but you lose portability.