Objects

  • Objects are instances of Classes, which are like a template of data (attributes) and functions (methods)
  • Objects are created and their contents, which are pulled from the class, is used in code
  • Examples below:

Creating a class with a method

public class Tennis { // class creation
    
    public void printPlayers(){   // simple method
        System.out.println("Nadal");
        System.out.println("Federer");
        System.out.println("Djokovic");
    }

    public static void main(String[] args){  // main class that runs
        Tennis myObject = new Tennis();    // CREATING AN OBJECT FROM TENNIS CLASS
        myObject.printPlayers();      // Use dot notation to reference methods/attributes from the class that the object is initialized from
    }   

}

Tennis.main(null);
Nadal
Federer
Djokovic

Inheritance

  • Extending another Class inherits all of the methods and attributes from that class.
  • Class being extended is called the super class
  • Class extending is called subclass
public class TennisTwo extends Tennis{  // extending Tennis class
    public TennisTwo(){    // TennisTwo now has all of Tennis's methods
        super();
    }

    public static void main(String[] args){  // main class that runs
        TennisTwo myObjectTwo = new TennisTwo();    // CREATING AN OBJECT FROM TENNISTWO CLASS
        myObjectTwo.printPlayers();      // Use dot notation to reference methods/attributes from the INHERITED class
    }
}

TennisTwo.main(null);
Nadal
Federer
Djokovic

HACK - Console Menu

Click Here to Go to Hack

Objects Used

  • Makes Object from Scanner Class to obtain inputs / menu selection from User
  • Use System Class, to call static methods System.out.print and System.out.println to output to console
  • User Math Class, to call static method Math.random() to generate random number