Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

A class defines the blueprint for a data format, kind of like a template. They encapsulate data and the methods that operate on that data. Following the school example, you might have a Student class, a Course class, and a Teacher class.

In object-oriented programming (OOP), a class is a blueprint or template for creating objects. It defines the structure, behavior, and attributes of objects that belong to a specific type. A class in OOP aims to provide a reusable template that encapsulates data and related functionality. Here are the key purposes of a class:

  1. Encapsulation: A class encapsulates the data (attributes) and behavior (methods) that define the objects. It hides the internal implementation details and exposes only the necessary interface to interact with the objects. Encapsulation helps in maintaining data integrity, code organization, and modularity.

  2. Abstraction: A class allows for abstraction by capturing objects' essential characteristics and behaviors while hiding unnecessary details. It defines a higher-level concept or entity representing a real-world object or concept. Abstraction helps in simplifying complex systems and focusing on the relevant aspects.

  3. Inheritance: Classes can inherit properties and behaviors from other classes through inheritance. Inheritance allows the creation of derived classes (subclasses) that inherit the attributes and methods of a base class (superclass). It promotes code reuse, modularity, and hierarchical organization of classes.

  4. Polymorphism: Classes support polymorphism, which means that objects of different classes can be treated uniformly through common interfaces or base class references. Polymorphism enables flexibility and extensibility by allowing objects to take multiple forms and exhibit different behaviors based on their specific types.

  5. Modularity and Reusability: Classes promote modularity and reusability by encapsulating related data and functionality into self-contained units. They can be instantiated multiple times to create objects with similar characteristics. Classes can be reused in different parts of the codebase or in other projects, reducing code duplication and improving maintainability.

  6. Object Creation: Classes serve as factories for creating objects. They define the structure and initialization behavior for objects of a particular type. By instantiating a class, you can create multiple objects with similar attributes and behaviors.

Overall, the purpose of a class in OOP is to provide a blueprint for creating objects with defined attributes and behaviors, promoting encapsulation, abstraction, modularity, and code reuse. Classes are fundamental building blocks that enable the organization and modeling of complex systems by representing real-world entities or concepts in a software application.

...

Defining a class in Java involves several steps. Here's a basic example of how you can define a class in Java:

...

Code Block
languagejava
public class Main {
    public static void main(String[] args) {
        MyClass myObject = new MyClass("World");
        myObject.sayHello();  // Outputs: "Hello, World"
    }
}

Demo - Person Class

Code Block
languagejava
package Demo1PersonClass;
/**
 * @author Dr. Kevin Roark
 * Class contains attributes and methods for a Person
 */


public class Person {
    /*  -----------------------------------
     * Class Variables
      ----------------------------------- */
    private String firstName;
    private String lastName;
    private int age;

    /*
     * Constructors
      ----------------------------------- */
    public Person(String firstName, String lastName, int age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public Person() {
        super();
        this.firstName = "Unknown FirstName";
        this.lastName = "Unknown LastName";
        this.age = -1;
    }


    /* -----------------------------------
     * Getter/Setters
      ----------------------------------- */
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

	/*  -----------------------------------
	 * Class Methods
	   ----------------------------------- */
    /**
     * Function creates a string with labels and Object data
     * @return String
     */
    public String PrintInfo()
    {
        String myReturn = "";
        myReturn = "Name: " + this.getFirstName() + " " + this.getLastName() + "\n";
        myReturn += "Age: " + this.getAge();
        return myReturn;
    }
}

Driver

Code Block
languagejava
package Demo1PersonClass;

public class PersonDriver {
    public static void main(String[] args) {
        //Demo of using argument constructor
        Person myPersonOne = new Person("Kevin", "Roark", 61);
        System.out.println(myPersonOne.PrintInfo());

        //Demo of Default Constructor
        Person myPersonTwo = new Person();
        System.out.println(myPersonTwo.PrintInfo());

        //Now using the setters - give the default constructor some values
        myPersonTwo.setFirstName("Fred");
        myPersonTwo.setLastName("Flintstone");
        myPersonTwo.setAge(29);

        //now output the myPersonTwo that now has data
        System.out.println(myPersonTwo.PrintInfo());

    }
}

...