Classes

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:

public class MyClass { // Fields (or attributes) private String name; // Constructor public MyClass(String name) { this.name = name; } // Method public void sayHello() { System.out.println("Hello, " + name); } // Getter and Setter methods public String getName() { return name; } public void setName(String name) { this.name = name; } }

In the above Java class:

  • MyClass is the name of the class.

  • name is a private field (or attribute) of the class. It's private, which means it can only be accessed within the same class. No outside class can access this field directly. They have to use getter (getName()) and setter (setName()) methods.

  • MyClass(String name) is a constructor method. This is a special type of method that is used to initialize the object of the class. It's called when an object of the class is created.

  • sayHello() is a method of the class. This is a function defined inside the class that performs a certain action.

  • getName() and setName(String name) are getter and setter methods, used to read and modify the private variable name.

You would then create an object of this class and use its methods in another class, likely with a main method, like so:

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

Demo - Person Class

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