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