Skip to content

Object Oriented Programming Notes

dylanroman edited this page Mar 14, 2022 · 20 revisions

How to create a simple Java Class

  • Every Java program consists of a collection of classes.
  • A class is a template for creating a particular form of an object. Each object created by the template contains the same members, each of which is either a field (which can hold a value) or a method.
  • A method is an operation on the fields of the object and any values that are passed as arguments to the method. The objects created by a particular class template are called the instances or objects of that class. The methods are statements that return an output which then gives back the result to the caller. In this case, Entry is the caller and we are creating a constructor that holds the fields that we created.
class Entry {

  /* fields */ 
  String name;	
  String address;
  String phone;

  /* constructor */
  Entry(String n, String a, String p) {
    this.name = n;
    this.address = a;
    this.phone = p;
  }	 	 

  /* accessors */
  String getName() { return this.name; }
  String getAddress() { return this.address; }
  String getPhone() { return this.phone; }

}
  • three fields name and address and phone which must contain String values;
  • a constructor that specifies how these fields are initialized when an Entry object is constructed; and
  • three methods getName, getAddress, and getPhone that define accessors for extracting the corresponding fields from an Entry.

The basics of OOP (object-oriented programming)

  • OOP uses classes, objects, & methods to break down a bigger problem
  • makes things more efficient since we can "reuse" class attributes & methods through inheritance
  • a class gives the basic blueprint structure for objects
  • objects are instances of this class that share the same attributes and methods
  • ex: if there is a class car, an object could be a BMW

Example

  • you define a class "Student" with attributes such as name, grade, school and methods study() and exercise()
  • now you can create object instances of the class that are specific students -- these objects will have these same attributes and methods
class Student {

     /* fields */
     String name;
     int grade;
     String school;
     
     /* constructor */
     Entry(String n, int g, String s) {
         this.name = n;
         this.grade= g;
         this.school= s;
      }	 	 

     /* accessors */
     String getName() { return this.name; }
     int getGrade() { return this.grade; }
     String getSchool() { return this.school; }

    /*defining methods (example methods shown are 'void' type, so they don't return a value)*/
    public void study() { System.out.println("study") }
    public void exercise() { System.out.println("exercise") }
}
public class Main {
  public static void main(String[] args) {
     /* initializing an object */
     Student steve = new Student("Steve", 9, "Del Norte");
 
     /* calling a method */
     steve.study();
  }
}

Java vs. Python: The main differences

in Python content in Java
Screen Shot 2022-03-08 at 8 27 27 PM printing in terminal Screen Shot 2022-03-08 at 8 29 04 PM
Screen Shot 2022-03-08 at 9 19 33 PM classes Screen Shot 2022-03-08 at 9 14 43 PM
Screen Shot 2022-03-08 at 8 35 56 PM for loops: Screen Shot 2022-03-08 at 8 38 03 PM
Screen Shot 2022-03-08 at 8 55 45 PM arrays: In Java, You have to specify if teh array containers integers or letters Screen Shot 2022-03-08 at 8 44 47 PM
Screen Shot 2022-03-08 at 9 22 20 PM variables: in Java, you need to specify the variable with either String or int depending on what is the variable Screen Shot 2022-03-08 at 9 24 36 PM

Data types

  • Data types are easy ways to hold values for what you are trying to store. Data types are broken up into primitive data types and Non-primitive types (which are more complex and go more in-depth). In the example below we have a few examples of some primitive data types which hold/store some values. Using the main method, we are calling these data types and printing them to the console.
public class Main {
  public static void main(String[] args) {
    int myNum = 5;               // integer (whole number)
    float myFloatNum = 5.99f;    // floating point number
    char myLetter = 'D';         // character
    boolean myBool = true;       // boolean
    String myText = "Hello";     // String    
    System.out.println(myNum);
    System.out.println(myFloatNum);
    System.out.println(myLetter);
    System.out.println(myBool);
    System.out.println(myText);
  }
}

Java Class Structure

//Initialize class
public class myClass {

 //Initialize variables
 private String myString;
 private int myInt;

 //constructor
 public myClass(String myString, int myInt) {
  this.myString = myString;
  this.myInt = myInt;
 }

 //methods
 public int add(int value) {
  int output = myInt + value;
  return output;
 } 

 //Getters
 public String getMyString() {
  return this.myString;
 }

 public int getMyInt() {
  return this.myInt;
 }

 //Setters
 public void setMyString(String newString) {
  this.myString = newString;
 }

 public void setMyInt(int newInt) {
  this.myInt = newInt;
 }
}

Notes:

public class myClass {
  • Class name must be the same as file name
 //Initialize variables
 private String myString;
 private int myInt;
  • Private variables cannot be called outside of the class, similar to how variables in a python funciton can only be called within the function
  • Must specify variable type
  • Do not assign values to variables here
 //constructor
 public myClass(String myString, int myInt) {
  this.myString = myString;
  this.myInt = myInt;
 }
  • Constructor is how an object is created from a class
  • Parameters must have data/class types declared, otherwise they are identical to python
  • this. refers to class variable
 //methods
 public int add(int value) {
  int output = myInt + value;
  return output;
 } 
  • Methods are class functions
  • Methods can use class variables by using this.(variable name)
  • Must specify output variable type, if there is no return use void
 //Getters
 public String getMyString() {
  return this.myString;
 }

 public int getMyInt() {
  return this.myInt;
 }
  • Getters enable the ability to call a class variable outside the class
  • If a private variable is to be called outside of the class, a getter must be used
  • Match data type to data type of the private variable used
 //Setters
 public void setMyString(String newString) {
  this.myString = newString;
 }

 public void setMyInt(int newInt) {
  this.myInt = newInt;
 }
  • Setters change class variables
  • Setters are void because there is no return

Calling a java Class

myClass test = new myClass("test", 18);

test.add(2);

System.out.println(test.getMyString());

test.setMyString("new test");

Notes:

myClass test = new myClass("test", 18);
  • Instead of declaring test as a variable type, declare it as the class
  • myClass("test", 18); uses the constructor to initialize the class
  • new creates a new instance of the class
test.add(2);

System.out.println(test.getMyString());

test.setMyString("new test");
  • Use object name.method(parameter) to call a method
  • When calling an object's method, data type does not need to be specified, it is already specified when method was created in class