zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What are the differences between a superclass and a subclass?

Superclass: More general, provides base functionality | Subclass: More specific, inherits from superclass and adds/modifies functionality.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What are the differences between a superclass and a subclass?

Superclass: More general, provides base functionality | Subclass: More specific, inherits from superclass and adds/modifies functionality.

What are the differences between inheritance and composition?

Inheritance: 'is-a' relationship, tight coupling | Composition: 'has-a' relationship, loose coupling.

What are the differences between method overriding and method overloading?

Overriding: Same method signature in subclass and superclass | Overloading: Same method name but different parameters in the same class.

What are the differences between single inheritance and multiple inheritance?

Single Inheritance: A class inherits from only one superclass | Multiple Inheritance: A class inherits from multiple superclasses (not supported in Java directly).

What are the differences between static and dynamic typing?

Static typing: Type checking at compile time | Dynamic typing: Type checking at runtime.

What are the differences between public and private members in the context of inheritance?

Public: Accessible from anywhere | Private: Only accessible within the class where they are defined, not inherited directly.

What are the differences between using 'extends' and 'implements' keywords?

'extends': Used for inheritance between classes | 'implements': Used for a class to implement an interface.

What are the differences between an abstract class and an interface?

Abstract Class: Can have method implementations and instance variables | Interface: Only method signatures (before Java 8) and constant variables.

What are the differences between inheritance and aggregation?

Inheritance: Creates a strong 'is-a' relationship, subclass depends on superclass | Aggregation: Creates a 'has-a' relationship, weaker dependency.

What are the differences between using inheritance and creating separate, unrelated classes?

Inheritance: Promotes code reuse, establishes hierarchical relationships | Separate Classes: No inherent relationship, can lead to code duplication.

How is inheritance applied in GUI frameworks?

GUI components (e.g., buttons, text fields) are often created as subclasses of a base component class, inheriting common properties and behaviors.

How is inheritance used in creating different types of bank accounts?

A base 'Account' class can be a superclass for 'SavingsAccount' and 'CheckingAccount', inheriting common account properties and methods.

How can inheritance be applied in a game development context?

A base 'GameObject' class can be a superclass for different game entities like 'Player', 'Enemy', and 'Item', sharing common properties like position and collision detection.

How is inheritance applied in creating different types of employees in a company?

A base 'Employee' class can be a superclass for 'Manager', 'Developer', and 'SalesPerson', inheriting common employee attributes and methods.

How is inheritance used in representing different types of vehicles?

A base 'Vehicle' class can be a superclass for 'Car', 'Truck', and 'Motorcycle', inheriting common vehicle properties and methods.

How is inheritance applied in creating different shapes in a drawing application?

A base 'Shape' class can be a superclass for 'Circle', 'Rectangle', and 'Triangle', inheriting common properties like color and position.

How is inheritance used in representing different types of media files?

A base 'MediaFile' class can be a superclass for 'AudioFile', 'VideoFile', and 'ImageFile', inheriting common properties like file size and format.

How is inheritance applied in creating different types of users in a social media platform?

A base 'User' class can be a superclass for 'Admin', 'Moderator', and 'RegularUser', inheriting common user attributes and methods.

How is inheritance used in representing different types of geometric figures?

A base 'Figure' class can be a superclass for 'Square', 'Circle', and 'Triangle', inheriting common properties like area and perimeter calculations.

How is inheritance applied in creating different types of network connections?

A base 'Connection' class can be a superclass for 'WifiConnection', 'BluetoothConnection', and 'EthernetConnection', inheriting common connection properties and methods.

What does the following code output?

java
class A {
    public void printMessage() {
        System.out.println("Class A");
    }
}

class B extends A {
}

public class Main {
    public static void main(String[] args) {
        B b = new B();
        b.printMessage();
    }
}

Class A

Identify the error in the following code:

java
class Animal {
}

class Dog extends Animal, Pet {
}

Java does not support multiple inheritance of classes. A class can only extend one class. Remove either 'Animal' or 'Pet'.

What does the following code output?

java
class Vehicle {
    public String modelName = "Generic Vehicle";
}

class Car extends Vehicle {
    public String modelName = "Car Model";

    public void displayModel() {
        System.out.println(modelName);
    }

    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.displayModel();
    }
}

Car Model

Complete the code snippet to make 'Dog' a subclass of 'Animal':

java
class Animal {
}

class Dog ______ Animal {
}

extends

What does the following code output?

java
class Parent {
    public void display() {
        System.out.println("Parent class");
    }
}

class Child extends Parent {
    public void display() {
        System.out.println("Child class");
    }
}

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.display();
    }
}

Child class

Identify the error in the following code:

java
class A {
    private int x = 5;
}

class B extends A {
    public void printX() {
        System.out.println(x);
    }
}

The instance variable 'x' in class 'A' is private and cannot be accessed directly from subclass 'B'.

What does the following code output?

java
class Animal {
    public void makeSound() {
        System.out.println("Generic animal sound");
    }
}

class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        myAnimal.makeSound();
    }
}

Woof!

Complete the following code to call the superclass's constructor:

java
class A {
    public A(int x) {
    }
}
class B extends A{
    public B(int x){
      ______;
    }
}

super(x);

What does the following code output?

java
class A {
    public A() {
        System.out.println("A");
    }
}

class B extends A {
    public B() {
        System.out.println("B");
    }
}

public class Main {
    public static void main(String[] args) {
        B b = new B();
    }
}

A B

Identify the error in the following code:

java
class Animal {
    public final void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    public void eat() {
        System.out.println("Dog is eating");
    }
}

The method 'eat()' in class 'Dog' cannot override the final method 'eat()' in class 'Animal'.