AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Instances
  • Examples

Was this helpful?

  1. Classes and Object-Oriented Programming

Classes vs. Objects

It is important to remember that classes are just templates for creating new objects. Objects contain both a state and behavior, and is an instance of a class.

Instances

An instance is a specific version of an object that can differ in numerous ways.

Going back to the previous chapter, Rectangle, Animal, and Vehicle are all classes. These classes, as is, are considered a type, unless you create a specific version of the class.

In essence, if we create two different vehicles they would be specific instances of the Vehicle class.

Remember, An object is an instance of a class.

Examples

public class ExampleClass extends ConsoleProgram
{
  public void run()
  {
    // `Animal` is our class.

    // `myDog` and `myCat` are objects, because
    // they are specific instances of our `Animal` class.
    Animal myDog = new Animal("Cujo", true, 7);
    Animal myCat = new Animal("Kerby", true, 2);
  }
}
public class ExampleClass extends ConsoleProgram
{
  public void run()
  {
      // `Rectangle` is our class.

      //`mySquare` and `myRectangle` are objects, because
      // they are specific instances of our `Rectangle` class.
      Rectangle mySquare = new Rectangle(20, 20);
      Rextangle myRectangle = new Rectangle(5, 10);
  }
}
PreviousIntroduction To Classes and ObjectsNextUsing a Class as a Client

Last updated 5 years ago

Was this helpful?