External Exam Download Resources Web Applications Games Recycle Bin

Object Oriented Programming


OOP = programming paradigm. some defns:
Player class
a player has a username. if a username isnt supplied when creating an instance of this class, the username is set to "unknown" by the constructor when the object is first created:
//save as Player.java
public class Player {
//----variables:
      public String username;
      
//----constructors (so no return type):
      public Player(){ username = "unknown"; }
      public Player(String nameSupplied) { username = nameSupplied; }

//----methods:
      public void greet() {
        System.out.println("hello " + username);
      }
}
our program executes from the main procedure:
//save as mainGame.java
public class mainGame {  
    public static void main(String[] args) { 
       Player robert = new Player("3l1t3_5n1p3r");
       robert.greet();
       
       Player sally = new Player();
       sally.greet();
       sally.username = "xX_memelord_Xx";
       sally.greet();
    }
}