Chapter 5: Class Design Flashcards
(141 cards)
What is inheritance?
The process by which the subclass automatically any public or protected primitives, objects, or methods defined in the parent class.
If child X inherits from class Y, which in turn inherits from class Z, then what is X in relation to Z?
An indirect child or descendent.
What’s the difference between single inheritance and multiple inheritances? Does Java support both?
Single inheritance is when the the child and parent relationship is only one level. For example, you would not be able to inherit from a grandparent in this case.
Multiple inheritance is when a class can extend multiple parents.
Java does not support multiple inheritance, but it does support single inheritance.
Does Java support multiple levels of single inheritance? Can a child inherit from a grandparent?
yes
What’s the single exception to single inheritance?
Interfaces, a class can implement as many interfaces as you want.
What’s one way in java to prevent a class from being extended?
By marking it with the final modifier
What’s the syntax for inheritance?
public class ElephantSeal extends Seal{
}
Do children have access to private members? (fields and methods)
no
Which takes up more memory? the parent or the child?
The child. Because the child is both the parent AND more.
How does the default access modifier differ from the public keyword?
Public is available for any class to be used. Default access is only available to be used within the same package. Which differs from protected in that default access is not extended by inheritance. So a child does not have access to default access unless they parent and the child are in the same package.
Does this compile (in the same file)?
class Rodent {}
public class Groundhog extends Rodent{}
Yes. Fine as only one of the classes is marked public.
Does this compile (in the same file)?
public class Rodent {}
public class Groundhog extends Rodent{}
no. only one public class allowed per file.
True or false: There can be at most one public class or interface in a Java file?
True
What’s the only class in Java that doesn’t have any parent classes?
java.lang.Object
Does every class have at least one constructor?
Yes. There is a default no-argument constructor no matter what.
What is a super() constructor? Can it have arguments?
it is the constructor of any parent class. It must have arguments if the constructor has arguments.
Will this compile? Why or why not?
public class Zoo{
public Zoo(){
System.out.println(“Zoo created”);
super();
}
}
No. LIke the this() command, super() must be the first statement used in a constructor.
Will this compile? Why or why not?
public class Zoo{
public Zoo(){
super();
System.out.println(“Zoo created”);
super();
}
}
No. super() can only be the first statement in a constructor. It cannot be first and second, or first and third.
If a parent has defined one constructor, is the child forced to use it?
yes
if a parent has declared two constructors, does the child need to use both of the constructors?
No. It may use either of the declared constructors, it may use either valid constructor from the parent.
How does Java handle the collision between parent and child classes which methods with matching signatures?
You can override the method, or refer explicitly to the parent version with super.() So, let’s say the child has the same method signature if you want it to do the exact same thing as the parent, just do super.(method) to get the same behavior.
What’s the order of operations here? (Assume App2 extends App, and App3 extends App2)
public static void main(String…strings ) {
App app = new App();
App2 app2 = new App2();
App3 app3 = new App3();
}
}
the first line will call App’s empty constructor.
The second line will again call App’s empty constructor. then it will call App2’s version of the empty constructor.
The third line will call App’s constructor, then App2’s constructor, and then App3’s constructor.
What are three limitations to the child being able to override a parent class? (other than, of course, the method signatures not matching)
- The method int he child class must be at least as accessible or more accessible than the method in the parent class.
- The method in the child class may not throw new exceptions or exceptions broader than the exception thrown in the parent class.
- If the method returns a value, it must be the same or a sublclass of the method in the parent clas.
Remember, the child method must be at least as accessible as the parent.