A constructor is often used to initialize the fields to their default values or in the case of a parameterized constructor, to the values passed in to the constructor.
Determines the amount of space needed for an object and creates the object.
If two methods perform the same function, they can be named the same. However, the number of parameters, type of parameters, or order of parameter types must be different.
A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design?
Only Car should be a class. The number of doors, flag if it has air conditioning, and the average number of miles per gallon are attributes of a car so they belong in a Car class.
Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon.
The number of doors, flag if it has air conditioning, and the average number of miles per gallon are attributes of a car. Each of these is a simple value so they can just be fields of a Car class.
Use a class Car, with subclasses of Doors, AirConditioning, and MilesPerGallon.
A door is not a type of car. A flag for air conditioning is not a type of door, and a miles per gallon is not a type of air conditioning flag. Child classes need to be able to be substituted for the parent class.
Use classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.
A class Car canβt be a subclass of three different classes. Each class can only have one parent class. Also a car is not a type of door, air conditioning flag, or miles per gallon. Child classes need to be able to be substituted for the parent class.
A program is being written by a team of programmers. One programmer is implementing a class called Employee; another programmer is writing code that will use the Employee class. Which of the following aspects of the public methods and fields of the Employee class does not need to be known by both programmers?
Only the programmer of the Employee class must know how the public methods work. The programmer that is using the Employee class can just use the public methods and not worry about how they are implemented.
The programmer who writes the methods will need to know what the names are. The programmer who will use the public methods will also need to know the names of the methods in order to invoke them.
A bookstore is working on an on-line ordering system. For each type of published material (books, movies, audio tapes) they need to track the id, title, author(s), date published, and price. Which of the following would be the best design?
This will complicate the process of retrieving objects based on their type. Also if we need to add information that is specific to Book or Movie or AudioTape it would be best if these were subclasses of PublishedMaterial.
Create classes Book, Movie, and AudioTape with the requested fields.
This involves writing more code than is necessary (usually people copy and paste the shared code) and makes it harder to fix errors. It would be better to put common fields and methods in the superclass PublishedMaterial and have Book, Movie, and AudioTape be subclasses.
Create one class BookStore with the requested fields plus type.
The class name, BookStore, seems to imply the thing that keeps track of the store. This would be an appropriate class name for an object that handles the items in the Bookstore. However, for the published material, it would be better to use a superclass PublishedMaterial and subclasses for Book, Movie and AudioTape.
This is more classes than is necessary. Items such as Title, Price, ID, Author and DatePublished are simple variables that do not need a class of their own but should be fields in a PublishedMaterial superclass, with Movie, AudioTape and Book as subclasses.
Create the class PublishedMaterial with children classes of Book, Movie, and AudioTape.
We will need to get objects based on their type so we should create classes for Book, Movie, and AudioTape. They have common fields so we should put these in a common superclass PublishedMaterial.
public class Student
{
public String getFood()
{
return "Pizza";
}
public String getInfo()
{
return this.getFood();
}
}
public class GradStudent extends Student
{
public String getFood()
{
return "Taco";
}
}
Wonβt compile since GradStudent doesnβt have a getInfo method
Objects know what class they are created as and all methods are resolved starting with that class at run time. If the method isnβt found in that class the parent class is checked (and so on until it is found). So it will first look for getInfo in GradStudent and when it doesnβt find it it will look in Student. In getInfo it calls this.getFood. Again, it will first look for this method in GradStudent. It will find the getFood method there and return "Taco".
In object methods if you leave off the this. when invoking an object method it will be added for you by the compiler. The keyword this refers to the current object which is implicitly passed to all object methods.
Given the following class declarations, and EnhancedItem enItemObj =
new EnhancedItem(); in a client class, which of the following statements would compile?
public class Item
{
private int x;
public void setX(int theX)
{
x = theX;
}
// ... other methods not shown
}
public class EnhancedItem extends Item
{
private int y;
public void setY(int theY)
{
y = theY;
}
// ... other methods not shown
}
I. enItemObj.y = 32;
II. enItemObj.setY(32);
III. enItemObj.setX(52);
I is wrong because y is a private field and thus can not be directly accessed from code in a client class. II is correct because EnhancedItem has setY as a public method. III is correct because EnhancedItem inherits the public method setX from Item.
I is wrong because y is a private field and thus can not be directly accessed from code in a client class. II is correct because EnhancedItem has setY as a public method. III is correct because EnhancedItem inherits the public method setX from Item.
public class Test1
{
public void method1(Test2 v1, Test3 v2)
{
// rest of method not shown
}
}
public class Test2 extends Test1
{
}
public class Test3 extends Test2
{
}
The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3();
You canβt pass an object of class Test1 since it is not either an object of type Test2 or an object of type Test3. You can pass the specified type or an object that is a subclass of the specified type but Test1 is not a subclass of Test2 or Test3.
You canβt pass an object of class Test2 as a parameter of type Test3. Test2 is the parent class of Test3 not a subclass. You can pass an object of the specified type or an object of any subclass.
You canβt pass an object of class Test1 since it is not either an object of type Test2 or an object of type Test3. You can pass the specified type or an object that is a subclass of the specified type but Test1 is not a subclass of Test2 or Test3.
You canβt pass t2 as an object of type Test3 since it is an object of class Test2 and class Test2 is not either class Test3 or a subclass of class Test3. Class Test2 is the parent of class Test3.
Since method1 is a public method of class Test1 objects of any subclasses of Test1 can invoke the method. So, it can be invoked on t3 since it is an object of Test3 and this is a subclass of Test1. And, since method1 takes an object of class Test2 and Test3 as parameters. This actually means it can take an object of Test2 or any subclass of Test2 and an object of Test3 or any subclass of Test3. So it can take t3 which is an object of class Test3 as an object of Test2 since Test3 is a subclass of Test2.
If you have a parent class Animal that has a method speak() which returns: Awk. Cat has a speak method that returns: Meow. Bird does not have a speak method. Dog has a speak method that returns: Woof. Pig does not have a speak method. Cow has a speak method that returns: Moo. What is the output from looping through the array a created below and asking each element to speak()?
Objects keep a reference to the class that created them. So, even if you put them in an array of Animal objects, they know what they are and all methods are resolved starting with the class of the object. Bird and Pig do not override speak so the speak method in Animal will execute.
Methods are always resolved starting with the class of the object, so Cat, Cow, and Dog will all execute their overridden speak methods, so the output will be: Meow Moo Woof Awk Awk.
public class Car
{
private int fuel;
public Car() { fuel = 0; }
public Car(int g) { fuel = g; }
public void addFuel() { fuel++; }
public void display() { System.out.print(fuel + " "); }
}
public class RaceCar extends Car
{
public RaceCar(int g) { super(2*g); }
}
What is the result when the following code is compiled and run?
Car car = new Car(5);
Car fastCar = new RaceCar(5);
car.display();
car.addFuel();
car.display();
fastCar.display();
fastCar.addFuel();
fastCar.display();
The code compiles and runs with no errors, the output is 5 6 5 6
RaceCar, while it inherits methods from Car via inheritance, has a separate and different constructor that sets the initial fuel amount to 2 * g, thus in this case, fuel for fastCar is set to 10 initially.
The code compiles and runs with no errors, the output is: 5 6 10 11
The code compiles correctly, and because RaceCar extends the Car class, all the public methods of Car can be used by RaceCar objects. Also, a variable Car can refer to a Car object or an object of any subclass of Car. An object always knows the class that created it, so even though fastCar is declared to be a Car the constructor that is executed is the one for RaceCar.
The code compiles and runs with no errors, the output is 10 11 10 11
The variable car is a Car object, so the constructor used is not the same as the fastCar object which is a RaceCar. The car constructor does not change the passed in parameter, so it is set to 5 initially.
public class Book
{
public String getISBN()
{
// implementation not shown
}
// constructors, fields, and other methods not shown
}
public class Dictionary extends Book
{
public String getDefinition(String word)
{
// implementation not shown
}
// constructors, fields, and methods not shown
}
Assume that the following declaration appears in a client class.
Book b = new Dictionary();
Which of the following statements would compile without error?
I. b.getISBN();
II. b.getDefinition("wonderful");
III. ((Dictionary) b).getDefinition("wonderful");
Both I and III are correct. I is correct because variable b has been declared to be an object of the class Book so you can invoke any public methods that are defined in the Book class or in parents of Book. II is not correct because you canβt invoke methods in the Dictionary class directly on b since b is declared to be of type Book not type Dictionary and Dictionary is a subclass of Book not a parent class of Book. III is correct because you can cast b to type Dictionary and then invoke public methods in Dictionary.
You canβt invoke methods in the Dictionary class directly on b since b is declared to be of type Book not type Dictionary and Dictionary is a subclass of Book not a parent class of Book. The compiler checks that the method exists on the declared class type, not the run-time type of the object.
I is correct because variable b has been declared to be an object of the class Book so you can invoke any public methods that are defined in the Book class or in parents of Book. II is not correct because you canβt invoke methods in the Dictionary class directly on b since b is declared to be of type Book not type Dictionary and Dictionary is a subclass of Book not a parent class of Book. III is correct because you can cast b to type Dictionary and then invoke public methods in Dictionary.
You canβt invoke methods in the Dictionary class directly on b since b is declared to be of type Book not type Dictionary and Dictionary is a subclass of Book not a parent class of Book. The compiler checks that the method exists on the declared class, not the run-time class.
public class Base
{
public void methodOne()
{
System.out.print("A");
methodTwo();
}
public void methodTwo()
{
System.out.print("B");
}
}
public class Derived extends Base
{
public void methodOne()
{
super.methodOne();
System.out.print("C");
}
public void methodTwo()
{
super.methodTwo();
System.out.print("D");
}
}
Even though b is declared as type Base it is created as an object of the Derived class, so all methods to it will be resolved starting with the Derived class. So the methodOne() in Derived will be called. This method first calls super.methodOne so this will invoke the method in the superclass (which is Base). So next the methodOne in Base will execute. This prints the letter "A" and invokes this.methodTwo(). Since b is really a Derived object, we check there first to see if it has a methodTwo. It does, so execution continues in the Derived class methodTwo. This method invokes super.methodTwo. So this will invoke the method in the super class ( Base) named methodTwo. This method prints the letter "B" and then returns. Next the execution returns from the call to the super.methodTwo and prints the letter "D". We return to the Base class methodOne and return from that to the Derived class methodOne and print the letter "C".
This would be true if the object was created of type Base. But the object is really a Derived object. So all methods are looked for starting with the Derived class.
After the call to methodOne in the super class printing "A", the code continues with the implicit this.methodTwo which resolves from the current objectβs class which is Derived. Next, methodTwo in the Derived class is executed which then calls super.methodTwo which invokes println "B" from methodTwo in the Base class. Then the "D" in the Derived methodTwo is printed. Finally the program returns to methodOne in the Derived class are prints "C".
public class Point2D
{
public int x;
public int y;
public Point2D() {}
public Point2D(int x,int y)
{
this.x = x;
this.y = y;
}
// other methods
}
public class Point3D extends Point2D
{
public int z;
// other code
}
I. public Point3D()
{
}
II. public Point3D(int x, int y, int z)
{
super(x,y);
this.z = z;
}
III. public Point3D(int x, int y)
{
this.x = x;
this.y = y;
this.z = 0;
}
Point2D does have a constructor that takes an x and y value so this is okay. Also the call to super is the first line of code in the child constructor as required. However, both I and III are okay as well.
The x and y values in Point2D are public and so can be directly accessed by all classes including subclasses. Also there is a no-arg constructor in Point2D so the super no-arg constructor will be called before the first line of code in this constructor.
I is true because Point2D does have a no-arg constructor. II is true because Point2D does have a constructor that takes x and y. III is true because Point2D does have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fields x and y are public in Point2D and thus can be directly accessed by all classes.
Point2D does have a no-arg constructor and since the constructor in Point3D doesnβt have an explicit call to super as the first line of code in the constructor one will be added for the no-arg constructor. However, both II and III are okay as well.
For practice with inheritance see Chapter 9: Inheritance and Interfaces at the Practice-It website. This website requires a login but it is free. Click on the following link and open Building Java Programs and Chapter 9: Practice-Itβ1β
For practice with Free Response Questions see question 2 from 2010, question 4 from 2014, and questions 2 and 4 from the 2015 exam. See past free response questions by clicking on the following link: exam infoβ2β