CS201 - Spring 2014 - Class 7

  • Exercises

  • static
       - When you declare a class variable as "static" it means that no matter how many objects you create they all share the same variable. To be more specific, only one instance of this variable exists! In almost all situations, this is NOT what you want.
       - When you declare a method as static:
          - It means that it cannot access any non-static variables of a class
          - and, as such, you do NOT need an instance of the object to call the method
       - Here is a good rule of thumb: the only times you should be using static are:
          1) Constants: Since a constant can't change, it doesn't matter if all objects of a class share it.
          2) main: the main method has to be static.
          3) Stand-alone methods.
             - When we right the warm-up methods, these are stand-alone that are not part of an object-oriented design
             - The Math class in Java has a lot of static methods (http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html) for things like cos, sin, log, sqrt, etc.
             - Static methods generally do not interact with other methods or variables and can be called by themselves.

          For all other variables and methods (that means almost everything you right besides the warmup questions and main) the should NOT be declared static.

  • Calling static methods
       - Say I have two classes

          public class A{
             public static void print(){
                System.out.println("Here");
             }
          }

          public class B{
             public void print(){
                System.out.println("Here");
             }
          }

       - The way we call these methods is *very* different:
          - For the static method, we can just call it directly:
             - if we're inside the class:
                print();

             - if we're outside the class:
                A.print();

                (Notice that A is the name of the class)

             - In both cases we do NOT declare an object

          - For non-static methods (i.e. normal methods), we have to call them ON an object:
             B someB = new B();
             someB.print()

             - we cannot call a non-static method without an object

  • A note about Eclipse
       - Eclipse is a great tool!
       - I'll show you a number of tricks as the semester progresses
       - Eclipse will show you errors and sometimes suggest fixes
          - Don't just randomly fix things!
          - Make sure you understand what you're fixing and why
             - If you don't understand, figure out why. Come talk to myself, Colby or the tutors, or go back and look at examples from class and/or the reading.
       - red underline vs. yellow underline
          - red underline = problem (most likely syntax)
             - you need to fix these!
          - yellow underline = warning
             - good to try and fix these
             - but if you don't understand what it's warning you about, don't worry about it

  • inheritance
       - we can build a new class that is an extension of another class
       - the original class is said to be the superclass (or parent class) and the new class the subclass (or child class)
       - to do this, in the class header, you use the keyword "extends", e.g.

          public class B extends A{
             ...
          }

       - the subclass:
          - inherits all public methods from the superclass
             - e.g. given an object of type B, we can call any method that A had on B
          - can access any instance variables that are declare public *or* protected
       - additions in the subclass
          - you can then add any additional instance variables and methods in the subclass
             - these will be available normally in the subclass, but NOT in the superclass
          - if you define a method with the same "signature" as in the superclass, it will override it
             - a m

  • protected
       - labeling a variable as protected allows the child class to access that variable
       - labeling a method as protected allows the child class to access those methods


  • the Java class hierarchy
       - *everything* in Java is a subclass of some class (except for 1 class in Java)
       - you can only inherit from one class
       - if you don't inherit from a class, by default, you inherit from the class Object
       - You can see the object hierarchy at: http://docs.oracle.com/javase/7/docs/api/overview-tree.html


  • Look at InheritanceBasics code
       - We have a class called A that has a private and protected instance variable
       - Two public methods
       - B is a class that inherits from A
          - we indicate this with the keyword "extends"
       - B has all of the instance variables of A
          - however, it can only access those that are protected or public
          - in the resetInt method, we can access number
          - if we tried to access the text variable, we would get an error

  • super
       - if the superclass has a constructor that has parameters, then it needs information to be constructed
       - when a subclass extends a superclass, it, in essence, has a version of the superclass nested inside of it
       - because of this, when constructing the subclass, the superclass also need to be constructed
       - to call the superclass's construct, you use the keyword "super"
       - it has to be the *first* thing that happens in the constructor of the sublcass

  • types
       - an A object is of type A
       - a B object is of type B
       - a B object is also of type A!
       - For example, the following are all legal:
          A temp = new A(10, "blah");
          B temp = new B(10, "blah");
          A temp = new B(10, "blah");
       
          - but the following is not:
          B temp = new A(10, "blah");

  • look at the test method in the B class in InheritanceBasics code
       - What will be printed out when we run it?
          10
          0
          10
       - notice that the B class inherits all of the public methods from the A class
          - specifically, we call the getNumber method and it gets the method description from the A class
       - also note that even though we do not have any instance variables declared, we inherit all the instance variables from A (number and text)
       - If we tried to call the resetInt method on an A object, it won't work!

  • look at test2
       - What ill be printed out?
          test
       - We've declared a variable of type A, why can we assign a B object to it?
          - All B object have the A methods!
       - Why can't we can't the resetInt method?
          - Even though it's a B object (and we know it), the Java compiler can't guarantee it:
             For example:

             A a;

             if( rand.randInt(2) == 1 ){
                a = new A(10, "test");
             }else{
                a = new B(10, "test");
             }

             // we have no idea if a references an A or a B!

  • look at test3
       - What will be printed out?
          A1: I'm an A
          B1: I'm a B
          A2: I'm a B
       - Since A2 references an object of type B, then B's toString method gets called!   
          - When the methods are called, they are based on the object