Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.

Similar presentations


Presentation on theme: "Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods."— Presentation transcript:

1 Classes CS 162 (Summer 2009)

2 Parts of a Class Instance Fields Methods

3 Instance Fields Also called ‘data fields’ or ‘member variables’ Describe the essential properties for objects of the class type Naming convention: use noun or descriptive phrase

4 Methods Describe the actions that objects of the class type can perform Naming convention: use verb or action phrase

5 Access Modifiers Private: accessible only by methods of the same class – enforces encapsulation Public: accessible by other classes – Typically don’t have public member variables (violates encapsulation) – Public methods control access to private data (use getters and setters) Protected: accessible by the same class and its subclasses

6 Static Fields Data field that does not belong to any one object One instance of a static data field exists to be shared by all instances of the class Also called a ‘class variable’

7 Static Fields Example public class Circle { public static final double PI = 3.14159; private double radius; } // all circles have the same PI value, but each instance has its own radius

8 Initializing Static Fields Do nothing. Java automatically initializes static fields as follows: – Numbers -> 0 – Booleans -> false – Objects -> null Use an explicit initializer. This is done once when the class is loaded. – As in PI example

9 Static Constants (special case of a static fields) can be private or public Example: public static final double PI =3.14159; – Refer to this constant outside the class as: double pi = Math.PI; Notice ‘Math’ is a class name, not an instance.

10 Static Fields should be private, as they relate to the internal state of a class want to minimize the use of static fields

11 Static Methods “public static” methods can be invoked without instantiating an object – eg, the Java Math class has many static methods: double x = Math.sqrt(y); – Note there is no object that instantiates the Math class in this statement.

12 Modifiers static final (methods and variables) – instance fields: constant – methods: no child may override abstract (methods only) – no definition – must be overridden in any derived class

13 How to build a class Design the public interface. Comment it. Determine behavior as everyone else will see it. – eg, DeathStar PowerSystem. Design and implement the instance fields. Implement constructors and other methods.

14 Accessors and Mutators Mutator modifies an object on which it is invoked. – eg, myDeathStar.blowUp(); Accessor accesses information without making modifications. – eg, myDeathStar.countStormTroopers();

15 Method Overloading Use the same method name for two or more methods with different parameter lists – eg, the println method is overloaded: void println(String s) void println(int i) void println(double d) and so on… – method to use is determined at compile time based on argument types

16 Method Overloading Signature of each overloaded method must be unique. – Signature includes number, type, and order of parameters – Return type of method is not part of the signature Parameter names don’t matter at all.

17 Examples int add (int x, int y) -> OK int add (double x, double y) -> OK double add (int x, int y) -> not OK, return types not part of signature, signature not unique int add (int x, double y) -> OK double add (double x, int y) -> OK

18 Constructors A method that: – Allocates memory for the object – Initializes the data fields – Returns the address of an object (to be assigned to an object variable)

19 Constructor Properties Almost always public Same name as the class No return type, not even void Invoked by new

20 Default Constructor Every class automatically has a constructor: – public DeathStar () {} Initializes variables to defaults (numbers->0, bool->false, objects->null) Only if no other constructors are defined.

21 Overloaded Constructors As soon as you create any overloaded constructor, the default constructor goes away public DeathStar (Color trimColor, CarpetType carpetType, long numStormTroopers) { trim = trimColor; carpet = carpetType; for (int I = 0; I < numStormTroopers; i++) { stormTroopers[i] = new StormTrooper(); } }

22 this A special reference to ‘self’ (the current instantiation) of the calling object Can be referenced anywhere within the scope of the class definition (except by static methods – why not?) Cannot be referenced outside the class

23 Inner Classes If you have a class that serves a very limited purpose, you can make it an inner class Declared either: – Inside a method – Inside another class For trivial classes that are generally only used once

24 Packages A collection of related classes Add a package declaration at the top of your file. – package edu.oregonstate.eecs.cs162.assignment1; – package com.lucasarts.starwars; Each word is a part of the package hierarchy If you don’t specify a package, your file goes into the default package (which has no name)

25 Packages Suppose you want to use a class from a package. – Refer to it as. com.lucasarts.starwars.DeathStar myDeathStar = new com.lucasarts.starwars.DeathStar(); – Or, add an import line: import com.lucasarts.starwars.DeathStar; DeathStar myDeathStar = new DeathStar(); – Or, add an import line for all classes in a package: import com.lucasarts.starwars.*;

26 Packages You don’t need to import the classes in java.lang (happens automatically) Packages avoid name clashes. – For example, there is a Timer class in java.util and javax.swing. Which one are you referring to? Reverse your domain name to name packages.

27 Packages Each part of package name has its own directory in the source code – -> com -> lucasarts ->starwars -> DeathStar.java


Download ppt "Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods."

Similar presentations


Ads by Google