🏛️Classes and Objects

🏗️ What is a Class?

A class is essentially a template or blueprint that defines what objects within a Java program will look like and be able to do. Think of it like a cookie cutter - the class is the mold that objects get "baked" from during runtime.

Within a class you define variables and methods. Variables store data, like a cookie's shape and color. Methods are actions the objects can perform, like rotating or growing.

When you create a class, it doesn't actually make any objects yet. But any objects created with "new" will be of that class type.

For example:

public class Person {

  //Within the curly brackets is where you'll define your variables and methods.
  
}

The Person here is a class. It acts as a blueprint for creating Person objects.

🏛️ Rules for Classes and Objects in Java 🏛️

Classes and objects are fundamental concepts in Java's object-oriented programming paradigm. There are certain rules around defining and using classes and objects.

📜 Rules for Classes 📜

Some key rules for defining classes in Java are:

  • A class must have a name that is a valid identifier

  • The class body must be between opening and closing braces {}

  • A class can contain fields, methods, constructors, nested classes etc.

  • Fields and methods can have any access modifier (public, private, protected, default)

  • Only one public class per source file is allowed

For example:

public class Person {

  private String name;

  public Person(String name) {
    this.name = name;
  }

}

This defines a Person class with a private field and public constructor.

🎁 Rules for Objects 🎁

Some important rules related to objects are:

  • Objects must be instantiated using the new keyword before use

  • Data fields in an object can be accessed using the . operator

  • Methods of an object can be invoked using the . operator

  • The this keyword refers to the current object instance

  • Objects in Java are passed by reference, not value

For example:

Person person = new Person("John"); 

System.out.println(person.name); // Access field

person.walk(); // Call method

This creates a Person object and accesses its fields and methods.

So those are some of the key rules around defining and working with classes and objects in Java. Following these rules allows creating robust object-oriented code.

🤓 Key Notes

  • A class is declared using the class keyword

  • It describes what attributes and behaviors an object will have

  • Objects are created (instantiated) based on the class

  • The class is a template, objects are instances of the template

So in summary, a class provides a definition for the attributes and behaviors of objects that will be created from it. It's a blueprint describing what the object will contain.

💎 In Simple Terms

A class is like a mold for making objects - it defines what the objects made from that mold will be like.

Objects are created from a class mold that describes them. The class itself is just the template, not the actual object.

So in Java, you define classes which act as blueprints for objects!

🥽 What is an Object?

An object in Java is an instance of a class. It is created from a class blueprint and contains its own state and behaviors.

For example:

public class Person {
  // Class definition
}

Person obj = new Person(); // Object instantiation 

Here obj is an object of type Person that is instantiated from the Person class.

🍃 Key Notes

  • An object is an instance of a class

  • It is created via new keyword from a class

  • It has its own set of attributes and behaviors

  • Multiple objects can be made from a single class

So an object is basically a real world entity represented inside a program.

Class and Object Problems
  1. What is a class? (A template that defines common properties and behaviors of an object)

  2. What is an object? (An instance of a class)

  3. How do you define a class in Java? (Use the class keyword followed by the name)

  4. How do you create an object from a class? (Use the new keyword followed by the class name)

  5. How do you add data fields/attributes to a class? (Define variables within the class)

  6. How do you add methods to a class? (Define functions within the class using the public or private keyword)

  7. How do you access object attributes? (Use dot notation objectname.attribute)

  8. How do you access object methods? (Use dot notation objectname.method())

  9. What is a constructor? (A special method that creates and initializes an object)

  10. How do you pass values to a constructor? (Include parameters in the constructor definition and pass them when creating the object)

Last updated