Class

Description:

Class: a prototype with state (fields) and behavior (methods) from which objects are created.

Object: a software bundle of related states and behavior. It’s instantiated from class. Ex Dog terry = new Dog("Terry", "Pitbull", 4);

When to useClasses are to be used when you have a collection/set of related functions and/or variables that would be best served as one entity.

Exmaple:

Class:

public class Dog {
    String name;
    String breed;
    int age;
    
    public Dog(String name, String breed, int age){
        this.name = name;
        this.breed = breed;
        this.age = age;
    }
    
    public void bark(){
        System.out.println("Woof Woof");
    }
    
    public void run(){
        System.out.println(name + " is running");
    }
    
    public void jump(){
        System.out.println(name + " just jumped");
    }
}

Main class:

public class Main {
    
public static void main(String [] args){
        
        Dog
terry = new Dog("Terry", "Pitbull", 4);
        
        
terry.bark();
        
terry.run();
        
terry.jump();
        
    }
}

Results:

Woof Woof
Terry is running
Terry just jumped

Challenge Problem

Leave a Reply