Docsity

Lecture 2: Java & Javadoc, Schemes and Mind Maps of Design

Partial preview of the text

Download Lecture 2: Java & Javadoc and more Schemes and Mind Maps Design in PDF only on Docsity!

CS 62

Fall 2018

Alexandra Papoutsaki & William Devanny

Lecture 2: Java & Javadoc

Instance Variables

  • or member variables or fields
  • Declared in a class, but outside of any method, constructor or block
  • Each object has its own copy of the variable!
  • Invoked as: myObject.variableName

Local Variables

  • Declared in method, constructor or block
  • Destroyed after the execution of the method
  • No access modifier

Methods

  • A collection of grouped statements that perform a logical operation and control the behavior of objects
  • Syntax:
    • modifier return-type method-name(type parameter-name,…){…}
    • e.g., public int enrollInClass(int classID){…}
    • Signature: method name and the number, type, and order of its parameters. Not return type
  • Can also be static, therefore shared by all instances of a class
  • Can be overloaded (same name, different parameters)

Combination of variables and methods

  • Instance methods can access instance variables and instance methods directly.
  • Instance methods can access static variables and static methods directly.
  • Static methods can access static variables and static methods directly.
  • Static methods cannot access instance variables or instance methods directly—they must use an object reference. - “Cannot make a static reference to the non-static field” in main method
  • Static methods cannot use the this keyword as there is no instance for this to refer to.

Exercise: Bicycle.java

  • Write the class Bicycle that contains the following fields:
    • cadence
    • gear
    • speed
    • id
    • numberOfBicycles
  • Primitive types or objects?
  • Instance variables or static? Instantiate?

Exercise: Bicycle.java

  • Write the appropriate getters and setters for these variables

public int getID() { return id; } public static int getNumberOfBicycles() { return numberOfBicycles; } public int getCadence() { return cadence; } public void setCadence(int cadence) { this.cadence = cadence; } public int getGear(){ return gear; } public void setGear(int gear) { this.gear = gear; } public int getSpeed() { return speed; }

Exercise: Bicycle.java

  • Write a main method within your class
  • Print the total number of bicycles
  • Create an object (unknown) using the non-parameterized constructor
  • Print its gear field
  • Create an object (myBike) passing the following 3 arguments (2, 3, 5).
  • Print its speed
  • Print the total number of bicycles

Exercise: Bicycle.java public static void main (String args[]) { System.out.println(Bicycle.numberOfBicycles); Bicycle unknown = new Bicycle(); System.out.println(unknown.getGear()); Bicycle myBike = new Bicycle(2,3,5); System.out.println(myBike.getSpeed()); System.out.println(Bicycle.getNumberOfBicycles()); }

Inheritance

  • When you want to create a new class and there is already a class that includes some of the code you want your new class to have, you can derive the new class from the existing class à reuse code!
  • We say that a class extends or inherits another class
  • E.g., public class Car extends Vehicle
  • Car is a subclass of Vehicle
  • Vehicle is a superclass of Car
  • Car IS-A Vehicle Vehicle Car

Inheritance in Java

  • A subclass inherits all of the public and protected members of parent
  • Hiding: same name of variables or of static method between super and subclass
  • Overriding: same signature of instance methods between super and subclass
  • Single inheritance!
    • A class can only extend ONE AND ONLY ONE class
  • Multilevel inheritance
    • Class SUV extends class Car which extends class Vehicle Vehicle Car SUV

Example: Cat.java public class Cat extends Animal { public int legs = 4; public static String species = "Cat"; public static void testClassMethod() { System.out.println("The static method in Cat"); } public void testInstanceMethod() { System.out.println("The instance method in Cat"); } }

Hiding vs Overriding public static void main(String[] args) { Cat myCat = new Cat(); myCat.testClassMethod(); //invoking a hidden method myCat.testInstanceMethod(); //invoking an overridden method System.out.println(myCat.legs); //accessing a hidden field System.out.println(myCat.species); //accessing a hidden field }

  • Output: “The static method in Cat\nThe instance method in Cat\n4\nCat”
  • What you were expecting, right?