5.1 Defining Classes

    Cards (88)

    • In Java, a class is declared using the class keyword followed by the class name and curly braces to enclose the class body.body
    • Classes enable modularity by breaking code into separate, manageable units.

      True
    • Match the access modifier with its access level:
      public ↔️ Accessible from anywhere
      private ↔️ Accessible only within the class
      protected ↔️ Accessible within the class, subclasses, and the same package
    • The protected access modifier allows access within the class, subclasses, and the same package.
      True
    • Inheritance allows classes to inherit properties from other classes.

      True
    • Match the class components with their descriptions:
      Class Variables (Fields) ↔️ Represent the state of an object
      Methods ↔️ Represent the behavior of an object
    • What is the purpose of access modifiers in Java?
      Control visibility and accessibility
    • Classes are the blueprints of object-oriented programming. They define what an object is, its attributes, and what it can do (methods). Using classes enables modularity, making code easier to understand and maintain, and code reusability, reducing redundancy.modularity
    • The class name in Java should be descriptive and follow Java naming conventions.

      True
    • Arrange the key components of a class declaration in Java:
      1️⃣ class keyword
      2️⃣ Class Name
      3️⃣ Curly Braces {}
    • The public modifier allows unrestricted access, private restricts access to only within the class, and protected allows access within the class, subclasses, and the same package.package
    • What is the purpose of classes in object-oriented programming?
      Create blueprints for objects
    • What keyword is used to declare a class in Java?
      class
    • Think of classes as recipes for objects, where each instance is like a new cake made from the same recipe.

      True
    • Match the class declaration component with its description:
      class keyword ↔️ Indicates the start of a class definition
      Class Name ↔️ Identifies the class (e.g., Car)
      Curly Braces {} ↔️ Enclose the class body
    • In a class, variables represent the state of an object, while methods represent the behavior of an object.state
    • What does the public access modifier in Java allow?
      Unrestricted access
    • Classes promote modularity by organizing code into separate, manageable units
    • Curly braces are used to enclose the body of a class
    • Class variables are accessed using the object reference
    • Which access modifier allows access only within the class itself?
      private
    • A private member in Java is accessible only within its own class.

      True
    • What are the two main components of a method in Java?
      signature and body
    • Using getters and setters promotes encapsulation by hiding the internal representation of an object
    • Classes break code into separate, manageable units, making it easier to understand and maintain
    • Order the key benefits of using classes in object-oriented programming:
      1️⃣ Modularity
      2️⃣ Code Reusability
      3️⃣ Encapsulation
      4️⃣ Inheritance
    • What are curly braces used for in a class declaration in Java?
      Enclosing the class body
    • What do methods in a class represent?
      Behavior of an object
    • Match the access modifier with its access level:
      public ↔️ Accessible from anywhere
      private ↔️ Accessible only within the class
      protected ↔️ Accessible within the class, subclasses, and the same package
    • Which access modifier allows access within the class, subclasses, and the same package?
      protected
    • A private class member is accessible only within the class
    • What is the access level of `protectedVar` in `MyClass`?
      Protected
    • What is the purpose of a constructor in Java?
      Initialize objects
    • Constructors differ from regular methods in that they initialize objects
    • What does a method signature include in Java?
      Name, return type, parameters
    • Getters and setters provide controlled access to class variables
    • Match the benefit of using classes with its description:
      Modularity ↔️ Code is broken into separate, manageable units
      Code Reusability ↔️ Classes can be used multiple times to create new objects
      Encapsulation ↔️ Data and methods are grouped together, protecting the internal state of objects
      Inheritance ↔️ Classes can inherit attributes and methods from other classes
    • Classes define what an object is, its attributes (data), and what it can do (methods).methods
    • Class variables are accessed using the object reference (e.g., `myObject.fieldName`).

      True
    • The private access modifier in Java restricts access to only within the class
    See similar decks