1 / 8

CS 211 Classes, Objects

CS 211 Classes, Objects. Today’s lecture. Review chapter 5 go over exercises. Classes and Objects. What is a class? (What do we place in a class?) What is an object? (where do we create them?) How do we create a class? How do we create an object? How do we use an object?.

xander
Download Presentation

CS 211 Classes, Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 211Classes, Objects

  2. Today’s lecture • Review chapter 5 • go over exercises

  3. Classes and Objects • What is a class? (What do we place in a class?) • What is an object?(where do we create them?) • How do we create a class? • How do we create an object? • How do we use an object?

  4. Object Uniqueness • Multiple objects of one type can be created that are distinct. • Each occupies a separate spot in memory. // create many objects Knapsack knap1 = new Knapsack(); Knapsack knap2 = new Knapsack(); knap1.size = 5; knap2.size = 8; System.out.println(knap1.size);System.out.println(knap2.size);

  5. Objects are values Objects are just values of a particular type (the given class type). "Every expression has a type": class-definitions are types too! Object-yielding expressions (yield a value of a reference type): • constructor calls • methods with a return type that is a class Nothing Special… Arrays of a reference type work just like arrays of primitive types. Expressions involving reference types are still just expressions. Variables can store reference types.

  6. Default Constructors Default Constructor: Java provides a default constructor definition when none is present in a class: all instance variables get default values: primitive types get 0, 0.0, false, or ' '; reference types (class types) get the null value.null is a special value: it represents a value of any reference type but has no actual reference value (no instance variables, no methods). Attempting to use null like an actual object is a very common run-time error.

  7. Let’s go over the exercises

  8. Questions?

More Related