Core Java

Objects and Classes

Copyright © Cay S. Horstmann 2016

Understand the fundamental concepts of object-oriented programming

Why OOP?

Classes and Objects

Work with predefined classes

Using Predefined Classes

Object Variables

Working with LocalDate

Accessor and Mutator Methods

CalendarTest

Define your own classes

Defining Your Own Classes

class Employee
{
   // Fields
   private String name; 
   private double salary;
   private LocalDate hireDay;

   // Constructors
   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      hireDay = LocalDate.of(year, month, day);
   }

   // Methods
   public String getName() { return name; }
   . . .
}

Constructors

Implicit and Explicit Parameters

EmployeeTest

Benefits of Encapsulation

Understand advanced concepts of classes in Java

Final and Static Fields

Static Methods

StaticTest

Understand parameter passing in Java

Call by Value

Call with Object References

Object References Are Passed by Value

ParamTest

Learn more about object construction

Overloading

Default Construction

Field Initialization

Construction Parameter Names

Calling Another Constructor

Initialization Blocks

ConstructorTest

Work with packages and imports

Packages

Imports

Static Imports

Adding a Class to a Package

PackageTest

Package Scope

The Class Path

Use the javadoc utility to produce class documentation

Documentation Comments

Method Comments

/**
* Raises the salary of an employee. 
* @param byPercent the percentage by which to raise the salary (e.g. 10 means 10%)
* @return the amount of the raise
*/
public double raiseSalary(double byPercent)
{
   double raise = salary * byPercent / 100;
   salary += raise;
   return raise;
}

Class and Field Comments

/**
 * A {@code Card} object represents a playing card, such
 * as "Queen of Hearts". A card has a suit (Diamond, Heart,
 * Spade or Club) and a value (1 = Ace, 2 . . . 10, 11 = Jack,
 * 12 = Queen, 13 = King)
 */
public class Card
{
   /**
    * The "Hearts" card suit
    */
   public static final int HEARTS = 1;
   ...
}

Other Comments

Comment Extraction

Design classes effectively

Class Design Hints

Class Design Hints