Creating a Class, describe Naming Conventions

Class can be created using the class keyword Class should be defined using upper camelcase (camelcase but first letter capital)

// creating a class
class MyClass {

}

Constructor

Constructor is called whenver the object is created, usually initializes fields Does not return anything because the object is automatically given to the user when constructor is called

class MyClass {
    int prop1;
    int prop2;
  
    // Constructor here
    public MyClass (int prop1input, int prop2input) {
      // setting properties using this to reference prop1 & prop2 of the object
      this.prop1 = prop1input;
      this.prop2 = prop2input;
    }
  
    public static void main (String[] args) {
      MyClass obj = new MyClass(1, 2);
    }
  }
  
  MyClass.main(null);

Accessor Methods

Used to get properties of an object from the outside the class definition Getters can be applied on only the properties which should be accessed outside the class

class MyClass {
    int prop1;
    int prop2;
  
    // Constructor here
    public MyClass (int prop1input, int prop2input) {
      // setting properties using this to reference prop1 & prop2 of the object
      this.prop1 = prop1input;
      this.prop2 = prop2input;
    }
  
    // getter allows outside class to access prop1
    public int getProp1() {
      return this.prop1;
    }
  
    public static void main (String[] args) {
      MyClass obj = new MyClass(1, 2);
  
      // using getter to access prop1
      System.out.println(obj.getProp1());
    }
  }
  
  MyClass.main(null);
1

Mutator (Modifier) Method (Setter)

typically a void method that changes value of instance variables or static variables void methods do not return values Must be used when different classes need to modify instance variables

// always public void
// setVariable (naming convention)
// parameter type must match type of instance variable being modified

public void setVariable(String variable){
    this.variable = variable;
}

Access modifiers

Access modifiers control whether properties and methods can be accessed outside the class The public means the property/method is accessible outside while if it is private it is not accessible

class MyClass {
    // prop1 cannot be directly accessed
    private int prop1;
  
    // prop2 can be directly accessed
    public int prop2;
  
    // Constructor here
    public MyClass (int prop1input, int prop2input) {
      // setting properties using this to reference prop1 & prop2 of the object
      this.prop1 = prop1input;
      this.prop2 = prop2input;
    }
  
    // getter allows outside class to access prop1
    public int getProp1() {
      return this.prop1;
    }
  
    // setter allows outside class to set prop1
    public void setProp1 (int propVal) {
      this.prop1 = propVal;
    }
  
    public static void main (String[] args) {
      MyClass obj = new MyClass(1, 2);
  
      // would throw error
      // obj.prop1 = 10;
  
      // works (because public)
      obj.prop2 = 11;
      System.out.println(obj.prop2);
    }
  }
  
  MyClass.main(null);

This keyword

The "this" keyword allows you to access properties of the class

Standard Methods

toString() Method

overridden method that provides description of a specific object (ie stored values) If System.out.print or System.out.println() is passed an object, that object's toString() method is called ==> returned string is printed

// method header always stays the same
// always returns string
// no parameters

public String toString(){

}

Equals()

Equals() is considered as a method in Java. 2. It is majorly used to compare the reference values and objects. It is used to compare the actual content of the object.

public boolean equals  (Object obj)

// This method checks if some other Object
// passed to it as an argument is equal to 
// the Object on which it is invoked.

Hashcode()

It returns the hashcode value as an Integer. Hashcode value is mostly used in hashing based collections like HashMap, HashSet, HashTable….etc. This method must be overridden in every class which overrides equals() method.

public int hashCode()

// This method returns the hash code value 
// for the object on which this method is invoked.

Big O Notation

Big O notation is a way of describing the accuracy and efficiency of an algorithm. We can apply it to: Hash map, Binary Search, Single loop, Nested Loop

Polymorphism

In Java, polymorphism refers to the ability of a class to provide different implementations of a method, depending on the type of object that is passed to the method. To put it simply, polymorphism in Java allows us to perform the same action in many different ways.