Class, Object, Packages, and Input/Output – Module 02 Java SBLC

Class, Object, Packages, and Input/Output – Module 02 Java SBLC

2.1 Class, Object, Data Members, Member Functions:

  • Class: A class is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit. For example:
  public class Car {
      String color;
      String model;
      int year;

      void drive() {
          System.out.println("The car is driving.");
      }

      void brake() {
          System.out.println("The car is braking.");
      }
  }
  • Object: An object is an instance of a class. It has states and behaviors defined by the class. For example:
  public class Main {
      public static void main(String[] args) {
          Car myCar = new Car();
          myCar.color = "Red";
          myCar.model = "Toyota";
          myCar.year = 2022;
          myCar.drive();
      }
  }
  • Data Members: Data members (or fields) are variables that hold the state of an object. They are defined in a class. For example, color, model, and year in the Car class are data members.
  • Member Functions: Member functions (or methods) are functions that define the behaviors of an object. They are also defined in a class. For example, drive() and brake() in the Car class are member functions.

Constructors, Types, Static Members, and Functions:

  • Constructors: Constructors are special methods invoked when an object is created. They initialize the object. Constructors have the same name as the class and do not have a return type. For example:
  public class Car {
      String color;
      String model;
      int year;

      Car(String color, String model, int year) {
          this.color = color;
          this.model = model;
          this.year = year;
      }

      void drive() {
          System.out.println("The car is driving.");
      }
  }

  public class Main {
      public static void main(String[] args) {
          Car myCar = new Car("Red", "Toyota", 2022);
          myCar.drive();
      }
  }
  • Types of Constructors:
    • Default Constructor: Automatically provided by Java if no other constructors are defined.
public class Car {
    Car() {
        // Default constructor
    }
}


  • Parameterized Constructor: Takes arguments to initialize the object.

public class Car {
    Car(String color, String model, int year) {
        this.color = color;
        this.model = model;
        this.year = year;
    }
}

Static Members and Functions:

Static Members: Static variables belong to the class rather than any specific instance. They are shared among all instances of the class

public class Car {
    static int numberOfCars;
    Car() {
        numberOfCars++;
    }
}

Static Functions: Static methods belong to the class and can be called without creating an instance of the class.

public class Car {
    static void displayTotalCars() {
        System.out.println("Total cars: " + numberOfCars);
    }
}

public class Main {
    public static void main(String[] args) {
        Car.displayTotalCars();
    }
}

Method Overloading:

  • Method overloading allows a class to have more than one method with the same name but different parameters (different type or number of parameters).
  public class MathUtils {
      int add(int a, int b) {
          return a + b;
      }

      double add(double a, double b) {
          return a + b;
      }
  }

  public class Main {
      public static void main(String[] args) {
          MathUtils utils = new MathUtils();
          System.out.println(utils.add(5, 3));      // Outputs 8
          System.out.println(utils.add(5.5, 3.5));  // Outputs 9.0
      }
  }

Packages in Java, Types, User-Defined Packages:

  • Packages: Packages are used to group related classes and interfaces, providing access protection and namespace management.
  • Built-in Packages: Java provides several built-in packages, like java.lang, java.util, java.io, etc.
  • User-Defined Packages: You can create your own packages to organize your classes. For example:
// In file Car.java
package vehicles;
public class Car {
    // Class implementation
}

// In file Main.java
import vehicles.Car;
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        // Use myCar
    }
}

Input and Output Functions in Java:

  • Java provides several classes for handling input and output operations. Two commonly used classes are BufferedReader and Scanner.

BufferedReader Class:

  • The BufferedReader class reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines.
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.IOException;

  public class Main {
      public static void main(String[] args) throws IOException {
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter your name: ");
          String name = reader.readLine();
          System.out.println("Your name is " + name);
      }
  }

Scanner Class:

  • The Scanner class is used to parse primitive types and strings using regular expressions. It’s commonly used for reading input from the console.
  import java.util.Scanner;

  public class Main {
      public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.println("Enter your age: ");
          int age = scanner.nextInt();
          System.out.println("Your age is " + age);
      }
  }
Team
Team

This account on Doubtly.in is managed by the core team of Doubtly.

Articles: 475