classes and Objects - Notes by Shariq SP

Classes and Objects in Java

In Java, a class serves as a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have. Let's explore how to write a class and create objects, along with important details and interview-based questions.

Class Example

To create a class in Java, you use the class keyword followed by the class name. Here's an example of a simple class:


        public class Car {
            // Attributes
            String brand;
            String model;
            int year;
    
            // Constructor
            public Car(String brand, String model, int year) {
                this.brand = brand;
                this.model = model;
                this.year = year;
            }
    
            // Method
            public void displayDetails() {
                System.out.println("Brand: " + brand);
                System.out.println("Model: " + model);
                System.out.println("Year: " + year);
            }
        }
        

Object Example

To create an object of a class, you use the new keyword followed by the class name and constructor (if any). Here's how you create an object of the Car class:


        Car myCar = new Car("Toyota", "Corolla", 2022);
        

The myCar object now has its own set of attributes (brand, model, year) and can invoke methods defined in the Car class.

Important Details about Classes and Objects

  • Class: Blueprint for creating objects, defines attributes and methods.
  • Object: Instance of a class, encapsulates data and behavior.
  • Constructor: Special method used for initializing objects, invoked when an object is created.
  • Attributes: Variables declared within a class to represent data/state.
  • Methods: Functions defined within a class to perform operations on data.
  • Encapsulation: Binding data and methods that operate on that data within a single unit (class).
  • Inheritance: Mechanism where a class inherits properties and behaviors from another class.
  • Polymorphism: Ability of objects to take on different forms or behaviors based on the context.
  • Interview Questions:
    1. What is a class in Java?
    2. How do you define attributes and methods in a class?
    3. Explain the concept of constructor in Java.
    4. What is the difference between a class and an object?
    5. How do you create an object of a class in Java?
    6. What is encapsulation and why is it important?
    7. Can you inherit a constructor from a superclass?

Multiple Choice Questions (MCQs)

  1. Which keyword is used to define a class in Java?
    1. class
    2. object
    3. new
    4. instance
  2. What is the purpose of a constructor in Java?
    1. To initialize objects
    2. To declare attributes
    3. To define methods
    4. To create classes
  3. How do you create an object of a class in Java?
    1. create keyword
    2. make keyword
    3. new keyword
    4. instanceOf keyword
  4. What is encapsulation in Java?
    1. Binding data and methods within a class
    2. Creating multiple instances of a class
    3. Accessing attributes directly
    4. Defining subclasses
  5. Which OOP concept allows a class to inherit properties and behaviors from another class?
    1. Inheritance
    2. Polymorphism
    3. Encapsulation
    4. Abstraction