Class Relationships

Help Questions

AP Computer Science A › Class Relationships

Questions 1 - 10
1

USING STATIC VARIABLES

CONSIDER THE FOLLOWING JAVA CODE:

**public class Employee**

**{**

static int nextID = 1;

String name;

int employeeID;

``

Employee(String n){

name = n;

employeeID = nextID;

nextID++;

}

public String getName(){

return name;

}

public int getID(){

return employeeID;

}

public static void main(String[] args)
{

Employee emp3 = new Employee("Kate");

Employee emp1 = new Employee("John");

Employee emp2 = new Employee("Sandy");

System.out.println(emp1.getName() + " ID: " + emp1.getID());

System.out.println(emp3.getName() + " ID: " + emp3.getID());

}

**}**

WHAT WOULD BE THE CONSOLE OUTPUT?

John ID: 2

Kate ID: 1

John ID: 1

Kate ID: 2

John ID: 1

Sandy ID: 1

John ID: 1

Sandy ID: 2

Explanation

Static variables are associated with a class, not objects of that particular class. Since we are using a static class variable "nextID" and assigning that to employeeID as a new employee is created and then increasing it, the class variable is updated for the whole class.

In our code, we have emp3 being created first. This means that Kate is created with an employeeID equaling the current value of nextID (which is 1 in this case). Afterwards, the nextID value increases by 1. Next emp1 is created with the name of John and an employeeID equaling to the current value of nextID (which became 2 due to the increased that occurred when Kate was created). Lastly, emp2 is created with the name of Sandy and an employeeID equaling the current value (at that point in the code) of nextID. Meaning Sandy has an employeID value of 3.

Therefore in the first System.out. print statement when we print emp1, we get: John ID: 2

and in the second System.out. print statement when we print emp3 we get:

Kate ID: 1

2

USING STATIC VARIABLES

CONSIDER THE FOLLOWING JAVA CODE:

**public class Employee**

**{**

static int nextID = 1;

String name;

int employeeID;

``

Employee(String n){

name = n;

employeeID = nextID;

nextID++;

}

public String getName(){

return name;

}

public int getID(){

return employeeID;

}

public static void main(String[] args)
{

Employee emp3 = new Employee("Kate");

Employee emp1 = new Employee("John");

Employee emp2 = new Employee("Sandy");

System.out.println(emp1.getName() + " ID: " + emp1.getID());

System.out.println(emp3.getName() + " ID: " + emp3.getID());

}

**}**

WHAT WOULD BE THE CONSOLE OUTPUT?

John ID: 2

Kate ID: 1

John ID: 1

Kate ID: 2

John ID: 1

Sandy ID: 1

John ID: 1

Sandy ID: 2

Explanation

Static variables are associated with a class, not objects of that particular class. Since we are using a static class variable "nextID" and assigning that to employeeID as a new employee is created and then increasing it, the class variable is updated for the whole class.

In our code, we have emp3 being created first. This means that Kate is created with an employeeID equaling the current value of nextID (which is 1 in this case). Afterwards, the nextID value increases by 1. Next emp1 is created with the name of John and an employeeID equaling to the current value of nextID (which became 2 due to the increased that occurred when Kate was created). Lastly, emp2 is created with the name of Sandy and an employeeID equaling the current value (at that point in the code) of nextID. Meaning Sandy has an employeID value of 3.

Therefore in the first System.out. print statement when we print emp1, we get: John ID: 2

and in the second System.out. print statement when we print emp3 we get:

Kate ID: 1

3

USING STATIC VARIABLES

CONSIDER THE FOLLOWING JAVA CODE:

**public class Employee**

**{**

static int nextID = 1;

String name;

int employeeID;

``

Employee(String n){

name = n;

employeeID = nextID;

nextID++;

}

public String getName(){

return name;

}

public int getID(){

return employeeID;

}

public static void main(String[] args)
{

Employee emp3 = new Employee("Kate");

Employee emp1 = new Employee("John");

Employee emp2 = new Employee("Sandy");

System.out.println(emp1.getName() + " ID: " + emp1.getID());

System.out.println(emp3.getName() + " ID: " + emp3.getID());

}

**}**

WHAT WOULD BE THE CONSOLE OUTPUT?

John ID: 2

Kate ID: 1

John ID: 1

Kate ID: 2

John ID: 1

Sandy ID: 1

John ID: 1

Sandy ID: 2

Explanation

Static variables are associated with a class, not objects of that particular class. Since we are using a static class variable "nextID" and assigning that to employeeID as a new employee is created and then increasing it, the class variable is updated for the whole class.

In our code, we have emp3 being created first. This means that Kate is created with an employeeID equaling the current value of nextID (which is 1 in this case). Afterwards, the nextID value increases by 1. Next emp1 is created with the name of John and an employeeID equaling to the current value of nextID (which became 2 due to the increased that occurred when Kate was created). Lastly, emp2 is created with the name of Sandy and an employeeID equaling the current value (at that point in the code) of nextID. Meaning Sandy has an employeID value of 3.

Therefore in the first System.out. print statement when we print emp1, we get: John ID: 2

and in the second System.out. print statement when we print emp3 we get:

Kate ID: 1

4

What is the difference between extending and implementing?

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can extend only a single class but you can implement multiple interfaces.

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can implement only a single interface but you can implement multiple classes.

You can extend an interface and implement a subclass. Subclasses can be implemented as frequently as needed but you can only extend a single interface.

Explanation

By default, all classes are extensions of the Object class and therefore are extending a single class. Due to that concept, implementation was made with the idea that notion of only being able to extend a single class. Thus they allow you to implement an infinite amount of classes.

5

What is the difference between extending and implementing?

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can extend only a single class but you can implement multiple interfaces.

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can implement only a single interface but you can implement multiple classes.

You can extend an interface and implement a subclass. Subclasses can be implemented as frequently as needed but you can only extend a single interface.

Explanation

By default, all classes are extensions of the Object class and therefore are extending a single class. Due to that concept, implementation was made with the idea that notion of only being able to extend a single class. Thus they allow you to implement an infinite amount of classes.

6

What is the difference between extending and implementing?

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can extend only a single class but you can implement multiple interfaces.

Extend refers to a relation between a superclass and subclass. Implementing refers to an interface. You can implement only a single interface but you can implement multiple classes.

You can extend an interface and implement a subclass. Subclasses can be implemented as frequently as needed but you can only extend a single interface.

Explanation

By default, all classes are extensions of the Object class and therefore are extending a single class. Due to that concept, implementation was made with the idea that notion of only being able to extend a single class. Thus they allow you to implement an infinite amount of classes.

7

What is the relationship between the class Car and the class TwoDoor in the code snippet below?

class TwoDoor extends Car {

}

Car is the super class to TwoDoor

TwoDoor is the super class to Car

TwoDoor implements Car

Car implements TwoDoor

Explanation

Car is the super class to TwoDoor. Since the class TwoDoor extends the class Car, TwoDoor can use methods and variables from Car by calling super.

The answers that mention implements are incorrect because implementing refers to interfaces. For TwoDoor to implement car, the code would look like this:

class TwoDoor implements Car {

}

8

What is the relationship between the class Car and the class TwoDoor in the code snippet below?

class TwoDoor extends Car {

}

Car is the super class to TwoDoor

TwoDoor is the super class to Car

TwoDoor implements Car

Car implements TwoDoor

Explanation

Car is the super class to TwoDoor. Since the class TwoDoor extends the class Car, TwoDoor can use methods and variables from Car by calling super.

The answers that mention implements are incorrect because implementing refers to interfaces. For TwoDoor to implement car, the code would look like this:

class TwoDoor implements Car {

}

9

What is the relationship between the class Car and the class TwoDoor in the code snippet below?

class TwoDoor extends Car {

}

Car is the super class to TwoDoor

TwoDoor is the super class to Car

TwoDoor implements Car

Car implements TwoDoor

Explanation

Car is the super class to TwoDoor. Since the class TwoDoor extends the class Car, TwoDoor can use methods and variables from Car by calling super.

The answers that mention implements are incorrect because implementing refers to interfaces. For TwoDoor to implement car, the code would look like this:

class TwoDoor implements Car {

}

10

Describe the difference between overloading a method and overriding a method.

Overloading is the same method name and different parameters. Overwriting is rewriting the body of the definition.

Overwriting is the same method name and different parameters. Overloading is rewriting the body of the definition.

Overloading is the same method name, same parameters but the name of the parameter is different. Overwriting is calling the same method in the same class.

Overloaded methods and overridden methods both take in the same methods but overridden methods return a different parameter.

Explanation

When you override a method, you are completely replacing the functionality of the method by overshadowing the method in the parent child with the one in the child class.

Overloading a method just allows you to have the same method name to have multiple meanings or uses. An example is '+' operator. In languages like Java, you can use '+' to append strings, add doubles, or add integers.

Page 1 of 2
Return to subject