slug
type
status
category
summary
date
tags
password
icon

Key Points for Topics:

9.1 Creating Superclasses and Subclasses
  • Superclass: A class that is inherited by another class. It provides common behavior for multiple subclasses.
  • Subclass: A class that inherits properties and methods from another class, and can also have its own unique properties and methods.
  • Inheritance: Mechanism in object-oriented programming (OOP) where a new class derives from an existing class, inheriting its fields and methods.
  • Polymorphism: The ability to use a subclass object where a superclass object is expected, allowing the same method to behave differently depending on the object type.
  • Abstract Classes: A class that cannot be instantiated but can be subclassed; it may contain abstract methods (methods without implementation) that must be implemented in the subclass.
9.2 Writing Constructors for Subclasses
  • Constructors: Special methods that are called when an object is instantiated. In Java, constructors have the same name as the class.
  • Superclass Constructors: When creating a subclass, you can call the superclass constructor using super(), ensuring proper initialization of the inherited properties.
  • Default Constructor: A constructor with no parameters that initializes the object with default values.
  • Parameterized Constructor: A constructor that allows the initialization of an object with user-provided values.

Sample Code:

AP Style Multiple-Choice Questions (MCQs):

  1. What is the purpose of inheritance in object-oriented programming? A) To allow subclasses to reuse code from the superclass
    1. B) To make classes completely independent
      C) To reduce the complexity of classes
      D) To make the subclass more complex
  1. Which keyword is used to call a superclass constructor in a subclass? A) super()
    1. B) this()
      C) parent()
      D) call()
  1. What happens if a subclass does not define a constructor? A) It will inherit the superclass constructor
    1. B) It will throw a compilation error
      C) It will automatically use the super() constructor
      D) It will not be able to be instantiated
  1. Which of the following is true about the super() method in Java? A) It can only be called in the main method
    1. B) It must be the first statement in the subclass constructor
      C) It is used to call methods from the subclass
      D) It cannot be used in constructors
  1. What is a characteristic of a constructor in a Java class? A) It has no return type
    1. B) It always returns a value
      C) It is called explicitly in the main method
      D) It cannot accept parameters
  1. When creating a subclass, which of the following must be true? A) The subclass can have no methods of its own
    1. B) The subclass must use the super() keyword
      C) The subclass inherits all fields and methods from the superclass
      D) The subclass cannot call the superclass constructor
  1. Which of the following is the correct way for a subclass to call a parameterized constructor of its superclass? A) super();
    1. B) super(parameter);
      C) this(parameter);
      D) constructor(parameter);
  1. What will be the output of the following code?
    1. A) Animal speaks.
      B) Dog barks.
      C) Compilation error.
      D) Runtime error.
  1. Which of the following is a feature of polymorphism in object-oriented programming? A) A subclass can call methods from other classes
    1. B) A superclass can call methods from the subclass
      C) A method behaves differently based on the object type
      D) A class can have multiple constructors
  1. If a subclass does not provide a constructor, which constructor is used? A) A default constructor from the superclass
    1. B) A default constructor from the subclass
      C) A parameterized constructor from the superclass
      D) A constructor with no parameters from the subclass
  1. What is an abstract class in Java? A) A class that cannot be instantiated
    1. B) A class that must implement all methods
      C) A class that cannot be inherited
      D) A class with no constructors
  1. If a subclass needs to inherit a field from the superclass, which of the following is true? A) The field must be private in the superclass
    1. B) The field must be public in the superclass
      C) The field can be either protected or public
      D) The field must be static
  1. Which of the following is a correct statement about constructors in a subclass? A) The subclass constructor must always call the superclass constructor
    1. B) If no constructor is defined, a default constructor is automatically called
      C) The constructor is not called when an object of the subclass is created
      D) Constructors in subclasses can only be default constructors
  1. How does the @Override annotation affect the method in a subclass? A) It prevents the method from being overridden
    1. B) It ensures the method is inherited from the superclass
      C) It indicates that the method is overriding a superclass method
      D) It changes the method signature in the subclass
  1. Which of the following best describes the relationship between a superclass and a subclass? A) The subclass can access only public methods of the superclass
    1. B) The superclass has access to all methods of the subclass
      C) The subclass can access all private fields of the superclass
      D) The superclass has no influence over the subclass
  1. In a Java class, what does the this keyword refer to? A) The current instance of the class
    1. B) The superclass constructor
      C) A specific method in the class
      D) A reference to an external class
  1. When would you use the super() keyword in a constructor? A) When you want to call a constructor in a different class
    1. B) When you want to call the constructor of the superclass
      C) When you want to call a method in the subclass
      D) When you want to create an abstract class
  1. What happens if a superclass has an abstract method and the subclass does not implement it? A) The subclass will not compile
    1. B) The method will be called automatically from the superclass
      C) The program will run without error
      D) The subclass will inherit a default implementation
  1. Which of the following statements about inheritance is correct? A) A subclass can inherit multiple classes in Java
    1. B) A subclass can inherit from multiple superclasses
      C) A subclass can override methods of the superclass
      D) A superclass can inherit methods from the subclass
  1. In Java, what is the role of a constructor? A) To initialize an object’s fields
    1. B) To create an object
      C) To destroy an object
      D) To define the main method

 
 
Answers:
  1. A) To allow subclasses to reuse code from the superclass
  1. A) super()
  1. A) It will inherit the superclass constructor
  1. B) It must be the first statement in the subclass constructor
  1. A) It has no return type
  1. C) The subclass inherits all fields and methods from the superclass
  1. B) super(parameter);
  1. B) Dog barks.
  1. C) A method behaves differently based on the object type
  1. A) A default constructor from the superclass
  1. A) A class that cannot be instantiated
  1. C) The field can be either protected or public
  1. B) If no constructor is defined, a default constructor is automatically called
  1. C) It indicates that the method is overriding a superclass method
  1. A) The subclass can access only public methods of the superclass
  1. A) The current instance of the class
  1. B) When you want to call the constructor of the superclass
  1. A) The subclass will not compile
  1. C) A subclass can override methods of the superclass
  1. A) To initialize an object’s fields
CSA UNIT 8: 2D ArrayCSA UNIT 10: Recursion
Loading...
现代数学启蒙
现代数学启蒙
推广现代数学🍚
最新发布
Statistics Key  Concepts and Selected Questions (*_*)
2025-2-20
CSA UNIT 7: ArrayList
2025-2-20
CSA UNIT 6:  ARRAY
2025-2-20
CSA UNIT 10: Recursion
2025-2-20
CSA UNIT 9: Inheritance
2025-2-19
CSA UNIT 8: 2D Array
2025-2-19
公告
🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
-- 感谢您的支持 ---