classes are used to make objects and they have instance variables, constructors, methods, accessors/getters, mutators/setters, tester, objects

methods

methods can tell what an object to do

public static void main(String[] args){
    
}

access modifiers

  • public, can be accessed outside the class
  • private can only be called in the class

Constructors

  • initialize instance variable, set initial state of object by setting initial values
  • A constructor initializes an object, a constructor has no return value because it is not actually used by the code, instead it simply initializes the object.
public class Main {
    int x;
  
   
    public Main() { // constructor
      x = 5; 
    }
  
    public static void main(String[] args) {
      Main myObj = new Main(); 
      System.out.println(myObj.x); 
    }
  }
Main.main(null)
5

Accessor Method

  • lets objects outside of class get values of instance variables/ static variables
  • has return type of string, int, long
public String getVariable(){

}

Mutator Method

  • void method that changes value of instance/static variable, don't return value
  • modify instance variables

this Keyword

“This” refers to an object within a constructor, used in setters to set variables/parameters.

public class CheckingAccount{
    private int balance;
    

    public void setBalance(int newBalance){
      this.balance = newBalance;
    }
  }

Creating a Class

  • a class is an object constructor or something used to make objects
  • use the keyword class to create a class
  • classes can have more than one class, which is called a nested class
  • for the outside class, only public, abstract, strictfp, and final can be used to modify class, but inside, private, protected, and static can be used

Static variables, Class variables

Static variables/Class variables are variables once declared and initialized becoming a global variable, used for the runtime of the entire program. You cannot change them

main method, tester methods

Main/tester method is used to test a class with a specific input to validate its working efficiency.

public class Employee {

    private static double salary; //static variable
 
    public static final String DEPARTMENT = "Development ";
 
    public static void main(String args[]) { //main method
       salary = 1000;
       System.out.println(DEPARTMENT + "average salary:" + salary);
    }
 }
 Employee.main(null);
Development average salary:1000.0

Inheritance, extends

Inheritance/extends takes aspects of an existing class and replicates it within another class using the extends method, simplifying code by a lot.

class Vehicle {
    protected String brand = "Ford";         
    public void honk() {                     
      System.out.println("car");
    }
  }
  
  class Car extends Vehicle { //extends vehicle class
    private String modelName = "Mustang";    
    public static void main(String[] args) {
  
     
      Car myCar = new Car();
  
      
      myCar.honk();
  
      
      System.out.println(myCar.brand + " " + myCar.modelName);
    }
  }
  Car.main(null);
car
Ford Mustang

Subclass constructor, super Keyword

A subclass constructor forms after extending a previous class, and using the super(); method will take a method and replicate it in the new class.

class Animal {
    public void animalSound() {
      System.out.println("dog");
    }
  }
  
  class Dog extends Animal { 
    public void animalSound() {
      super.animalSound(); // Calls superclass method
      System.out.println("cat");
    }
  }
  
  public class Main {
    public static void main(String args[]) {
      Animal myDog = new Dog(); 
      myDog.animalSound(); 
    }
  }

Main.main(null);
dog
cat

access modifiers

  • Public access modifiers are accessible from anywhere in any class or program in Java, no restrictions
  • Protected classes are used when you want to access the class within the package, subclasses. Can use extends to it to access.
  • Private access modifiers are the least accessible; only accessible within the class itself.

private

class A
{
private void display()
    {
        System.out.println("text");
    }
}
 
class B
{
public static void main(String args[])
    {
        A obj = new A();
        obj.display();
    }
}
B.main(null);
|           obj.display();
display() has private access in A

protected

public class A
{
protected void display()
    {
        System.out.println("text");
    }
}
class B extends A
{
public static void main(String args[])
{
    B obj = new B();
    obj.display();
}
     
}
B.main(null);
text

public

public class A
{
public void display()
    {
        System.out.println("text");
    }
}
class B {
    public static void main(String args[])
    {
        A obj = new A();
        obj.display();
    }
}
B.main(null);
text

HW

public class StepTracker {
    private int days;
    private int activeDays;
    private int totalSteps;
    private int minActive;

    public StepTracker(int m) {
        minActive = m;
        days = 0;
        activeDays = 0;
        totalSteps = 0;
    }

    public int activeDays() {
        return activeDays;
    }

    public double averageSteps() {
        if (days == 0) {
            return 0.0;
        }
        return (double) totalSteps / days;
    }

    public void addDailySteps(int steps) {
        days++;
        totalSteps += steps;
        if (steps >= minActive) {
            activeDays++;
        }
    }
}