Ch 5 - Class Design Flashcards
true/false, the following code compiles: public class Zoo { public Zoo() { super(); System.out.println("Zoo created"); }}
true
What is the least amount of classes in a .java file you can have that are public?
none
true/false, the followng code compiles: public class Animal { private int age; public Animal(int age) { super(); this.age = age; }} public class Zebra extends Animal { public Zebra(int age) { super(Age); } public Zebra() { this(4); }}
true
true/false, the following code compiles: public class Zoo { public Zoo() { System.out.println("Zoo created"); super(); }}
false
How many interfaces marked public can you have in a single .java file?
1
The following class does not have an access modifier: class Rodent {} What does this mean?
It has the default package private modifier, which indicates the class can only be accessed by a class within the same package.
Any class that inherits another class is know as what?
A child class
true/false, the following code compiles: public class Animal { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; }} public class Lion extends Animal { private void roar() { System.out.println("The " + age + " year old lion says Roar!"); }}
false. Although the Lion class extends the Animal class, it does not have direct access to the “age” member of Animal.
What is the name of the object that all java classes inherit from?
java.lang.Object
What is referred to as the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class.
inheritance
How do you prevent a class from being inherited?
By using the ‘final’ specifier.
true/false, when the java compiler sees that your class doesn’t extend another class, it automatically inserts code to extend java.lang.Object.
true
In the following code, what is the line containing the word super() calling? public class Animal { private int age; public Animal(int age) { super(); this.age = age; }}
The parent constructor in java.lang.Object.
How many classes can one class directly inherit from?
Just one. However, multiple inheritance can be achieved by one class inheriting from another, which inherits from another, …
How many classes can a .java file contain?
As many as you want.
How many classes can have the public access modifier in a single .java file?
one
true/false, the following code compiles: public class Zoo { public Zoo() { // Calling the parent constructor super(); System.out.println("Zoo created"); }}
true
true/false, the following code compiles: public class Zoo { public Zoo() { // Calling the parent constructor super(); super(); System.out.println("Zoo created"); }}
false
true/false, the following code compiles: public class Animal { public Animal(String s) { } } public class Elephant { public static void main(String[] args) { Animal a = new Animal(); } }
false. Since the class Animal doesn't have a no argument constructor, the following line in the Elephant class does not compile: Animal a = new Animal();
true/false, in Java, the child constructor is always executed before the parent constructor.
false. The parent constructor is always executed before the child constructor.
What is the output of the following code? class Primate { public Primate() { System.out.println("Primate"); }} class Ape extends Primate { public Ape() { System.out.println("Ape"); }} public class Chimpanzee extends Ape { public static void main(String[] args) { new Chimpanzee(); }}
Primate
Ape
true/false, if no super() call is declared in a constructor, Java will insert a no-argument super() as the first statement of the constructor.
true
true/false, the following code compiles: public class Animal { public Animal(String s) { } } public class Elephant extends Animal { public Elephant() { super("Stephen"); } }
true
true/false, a child class may access the private members of the parent class.
false
true/false, the first statement of every constructor is a call to another constructor within the class using this() or a call to a constructor in the direct parent using this().
false. The first statement of every constructor is a call to another constructor within the class using this() or a call to a constructor in the direct parent using super().
true/false, you can call the parent constructor as well as another constructor in your same class from within one constructor, eg: public Animal(int age) { super("Stephen"); this("Stephen"); }
false. You can only call one or the other, not both.
true/false, the compiler automatically inserts a call to the no-argument constructor super() if the first statement is not a call to the parent constructor.
true
true/false, the super() call may be used after the first statement of the constructor
false. The super() call may not be used after the first statement of the constructor.
true/false, if the parent doesn’t have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor.
true
true/false, you can call multiple other constructors from within one constructor: public Animal(int age) { this("Stephen"); this(48); }
false. You can only make one other constructor call.
true/false, if the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor in the child class.
true
true/false, the following code compiles: public class Animal { public Animal(String s) { } } public class Elephant extends Animal { }
false. Since the Elephant class does not define a constructor, the compiler will try to insert a default no-argument one for it that includes a ‘super()’ call.
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + super.getAge()); System.out.print(" and " + super.size + " meters long"); System.out.print(" with " + this.numberOfFins + " fins"); }}
true
true/false, the following code compiles:
pulic Rabbit(int age) {
super();
super.setAge(10);
}
true
true/false, you can use the “super()” command from within a normal method to call a constructor.
false. You can only use “super()” from within a constructor.
What is the output of the following code? public class Canine { public double getAverageWeight() { return 50; } }} public class Wolf extends Canine { public double getAverageWeight() { return getAverageWeight() + 20; } public static void main(String[] args) { System.out.println(new Canine.getAverageWeight()); System.out.println(new Wolf().getAverageWeight()); }}
Infinite loop on this line: return getAverageWeight() + 20; This happens because we did not use the "super" keyword to explicitly call the getAverageWeight() method in the parent class.
true/false, when overriding a method, the method in the child class must be more accessible than the method in the parent class.
false. It must be at least as accessible or more accessible.
When inheriting a class, what must the parent class members’ access modifiers be set to for us to see them?
public or protected
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + getAge()); System.out.print(" and " + size + " meters long"); System.out.print(" with " + numberOfFins + " fins"); }}
true
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + super.getAge()); System.out.print(" and " + super.size + " meters long"); System.out.print(" with " + super.numberOfFins + " fins"); }}
false. The following line fails to compile:
System.out.print(“ with “ + super.numberOfFins + “ fins”);
The this and super keywords may both be used for methods or variables defined in the parent class, but only this may be used for members defined in the current class.
What is the difference between “super” and “super()”?
"super" is used to reference a member in the parent class, and it can be used in methods outside the constructor. "super()" is used to reference a constructor in the parent class and can only be called from within the child class constructor. It must be the first uncommented line in the constructor.
true/false, when overriding a method, the method in the child class can have a different signature as the method in the parent class.
false. It must have the same signature.
true/false, when overriding a method, if the method returns a value, it must be the same type of the method in the parent class.
false. It can be the same type or a subclass of the method in the parent class.
What is the output of the following code? public class Canine { public double getAverageWeight() { return 50; } }} public class Wolf extends Canine { public double getAverageWeight() { return super.getAverageWeight() + 20; } public static void main(String[] args) { System.out.println(new Canine.getAverageWeight()); System.out.println(new Wolf().getAverageWeight()); }}
- 0
70. 0
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + this.getAge()); System.out.print(" and " + this.size + " meters long"); System.out.print(" with " + this.numberOfFins + " fins"); }}
true
true/false, when overriding a method, the method in the child class may not throw a checked exception thta is new or broader than the class of any exception thrown in the parent class method.
true
true/false, the following code compiles:
pulic Rabbit(int age) {
super.setAge(10);
super();
}
false. When using “super()” to call a parent constructor, it must be the first uncommented line in the constructor.
true/false, the following code compiles: public class Bird { public void fly() { System.out.println("Bird is flying"); } public void eat(int food) { System.out.println("Bird is eating " + food + " units of food."); } } public class Eagle extends Bird { public int fly(int height) { System.out.println("Bird is flying at " + height + " meters"); return height; } pubic int eat(int food) { System.out.println("Bird is eating " + food + " units of food"); return food; } }
false. The “eat()” method is not successfully overridden here since the return types, void and int, are not covariant.
true/false, the following code compiles: public class InsufficientDataException extends Exception {} public class Reptile { protected boolean hasLegs() throws InsufficientDataException { throw new InsufficientDataException(); } protected double getWeight() throws Exception { return 2; }} public class Snake extends Reptile { protected boolean hasLegs() { return false; } protected double getWeight() throws InsufficientDataException { return 2; }}
true
true/false, the following code compiles: public class InsufficientDataException extends Exception {} public class Reptile { protected boolean hasLegs() { throw new InsufficientDataException(); } protected double getWeight() { return 2; }} public class Snake extends Reptile { protected boolean hasLegs() { return false; } protected double getWeight() throws InsufficientDataException { return 2; }}
false. The getWeight() in the child class throws an exception that the parent does not.
true/false, the following code compiles: public class Camel { public int getNumberOfHumps() { return 0; } } public class BactrianCamel extends Camel { protected int getNumberOfHumps() { return 2; } }
false. The getNumberOfHumps() method in the child class must be at least as accessible as the parent method.
true/false, the following code compiles: public class Bird { public void fly() { System.out.println("Bird is flying"); } } public class Eagle extends Bird { public int fly(int height) { System.out.println("Bird is flying at " + height + " meters"); return height; } }
true. The method fly is successfully overloaded.
true/false, the following code compiles: public class Camel { protected int getNumberOfHumps() { return 0; } } public class BactrianCamel extends Camel { protected int getNumberOfHumps() { return 2; } }
true. The getNumberOfHumps() method in the child successfully overrides the same method in the parent.
true/false, the following code compiles: public class Bird { public void eat(int food) { System.out.println("Bird is eating " + food + " units of food."); } } public class Eagle extends Bird { pubic int eat(int food) { System.out.println("Bird is eating " + food + " units of food"); return food; } }
false. The “eat()” method is not successfully overridden here since the return types, void and int, are not covariant.
true/false, a child method may hide or eliminate a parent method’s exception without issue.
true
true/false, the following code compiles: public class Camel { protected String getNumberOfHumps() { return "Undefined"; } } public class BactrianCamel extends Camel { private int getNumberOfHumps() { return 2; } }
false. The getNumberOfHumps() method in the child class is not as accessible to the same method in the parent class. Also, the return types are not covariant.
true/false, the following code compiles: public class InsufficientDataException extends Exception {} public class Reptile { protected boolean hasLegs() throws InsufficientDataException { throw new InsufficientDataException(); } protected double getWeight() throws InsufficientDataException { return 2; }} public class Snake extends Reptile { protected boolean hasLegs() { return false; } protected double getWeight() throws Exception { return 2; }}
false. The scope of the exception thrown in Snake.getWeight() is narrower than in Reptile.getWeight().
What is the output of the following code: public class Bear { public static void eat() { System.out.println("Bear is eating"); } } public class Panda extends Bear { public void eat() { System.out.println("Panda bear is chewing"); } public static void main(String[] args) { Panda.eat(); } }
Compiler error on this line:
public void eat() {
The compiler thinks you’ve trying to override a method that should be hidden.
What is it called when methods replace parent methods in the calls defined in the child class?
Method hiding.
true/false, for method hiding, the method in the child class must have the same signature as the method in the parent class.
true
true/false, in Java, in a child class, it is possible to override a private method in the parent class.
false. However, the child class can define its own version of the method with the same or modified signature.
true/false, for method hiding, the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
true
What is the output of the following code: public class Bear { public void eat() { System.out.println("Bear is eating"); } } public class Panda extends Bear { public static void eat() { System.out.println("Panda bear is chewing"); } public static void main(String[] args) { Panda.eat(); } }
Compiler error on this line:
public static void eat() {
The compiler thinks you’re trying to hide a method that should be overridden.
A child method replacing the parent method in calls defined in both the parent and child is called what?
Method overriding.
true/false, for method hiding, the method in the child class must have the same visibility than the method in the parent class.
false. It must be at least as or more accessible than the method in the parent class.
true/false, for method hiding, the method defined in the child class must be marked as static if it is marked as static in the parent class.
true.
true/false, the following code compiles: public class Camel { private String getNumberOfHumps() { return "Undefined"; } } public class BactrianCamel extends Camel { private int getNumberOfHumps() { return 2; } }
true. This code compiles without issue. If the method in the parent class were public or protected, the code would not compile.
true/false, for method hiding, if the method returns a value, it must be different than that from the method in the parent class.
false. It must be the same or a subclass of that of the parent class, aka covariant.
What is the output of the following code: public class Bear { public static void eat() { System.out.println("Bear is eating"); } } public class Panda extends Bear { public static void eat() { System.out.println("Panda bear is chewing"); } public static void main(String[] args) { Panda.eat(); } }
Panda bear is chewing
When a child class defines a static method with the same name and signature as a static method defined in a parent class, what is it called?
A hidden method
When in a child class, you can access the parent version of a hidden variable using what keyword?
super
Does the following code compile: public class Bird { public final boolean hasFeathers() { return true; } } public class Penguin extends Bird { public boolean hasFeathers() { return false; } }
no. It doesn’t matter if the method in the child class is marked “final” or not. If the method in the parent is marked that way, it cannot be overridden.
When a parent method is overridden by a child method, the parent method is never used unless explicitly called using what syntax?
super.method()
What is the output of the following code? class Marsupial { public boolean isBiped() { return false; } public void getMarsupialDescription() { System.out.println("Marsupial walks on two legs: " + isBiped()); } } public class Kangaroo extends Marsupial { public boolean isBiped() { return true; } public void getKangarooDescription() { System.out.println("Kangaroo hops on two legs: " + isBiped()); } public static void main(String[] args) { Kangaroo joey = new Kangaroo(); joey.getMarsupialDescription(); joey.getKangarooDescription(); } }
Marsupial walks on two legs: true
Kangaroo walks on two legs: true
true/false, you can hide a method in a parent class if it’s marked “final”.
false. You cannot hide a method in a parent class from the child class if it’s marked “final”.
true/false, java allows variables to be overridden.
false. They can be hidden but not overridden.
Does the following code compile: public class Bird { public boolean hasFeathers() { return true; } } public class Penguin extends Bird { public final boolean hasFeathers() { return false; } }
yes. It’s ok if the overriding method is marked “final” so long as the method being overridden is not marked “final”.
When working with overridden variables, if you’re referencing the variable from within the parent class, the variable in which class is used (parent or child)?
parent.
Does the following code compile: public class Bird { public boolean hasFeathers() { return true; } } public class Penguin extends Bird { public boolean hasFeathers() { return false; } }
yes
true/false, at runtime the child version of an overridden method is always executed for an instance regardless of whether the method call is defined in a parent or child class method.
true