Chapter 5: Class Design Flashcards
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.
What does it mean that the return type of the parent method and the child method must be covariant?
It means that the overridden version of the parent’s method must return something similar to the return type of the parent. So if the parent’s method returns an int, the child’s method cannot return a string.
Anytime you see an overridden method on the exam, first check to make sure it is truly being overridden and not overloaded. After that, check the access modifiers, return types, and any exceptions defined in the method are compatible with one another.
If the parent method has a declared exception, and the child doesn’t, will the method compile? (Assuming everything else is the same?) (overridden method)
Yes. As long as no new exception is defined, this isn’t a problem.
True or fals: A child’s version of a parent’s method can hide or eliminate a parent’s method exception without issue. (overridden method)
True
Is the child (overridden method) allowed to throw a more specific exception than the parent method?
Yes. As long as the more specific exception is a subclass of the parent’s method.
Is a child method’s (overridden method)exception allowed to be more generic than the parent’s exception declaration?
No. It can only go from less specific to more specific, from the top down.
Can a child’s overridden version of a parent’s method declare a method exception when the parent’s does not?
No.
Is it possible for a child class to override a private method from it’s parent?
No. Private methods and fields or only accessible within the class.
What’s the difference between an overridden method and a hidden method?
Essentially the only difference is that because it’s static you don’t need to use the @Override annotation. The method is bound to the class, not an object. So as far as the parent/child knows, it isn’t really there.
What happens if you attempt to hide a method on a child, but the method isn’t marked static on both the parent and the child?
Will not compile. At that point, you need to make both methods static to hide it, or non static and override it.
Can you override a method if it is marked final?
No. Final methods cannot be overridden.
Can you override a hidden method if it has been marked final?
No.
Can you mark an overridden version of a parent’s method final on the child?
Yes. But if the parent is marked final you cannot override the method at all.
Why is it important to not that app2 build’s it’s own copy of app, and that app3 build it’s own copy of app2 and app, that are not the same as the copy made by app2?
public static void main(String…strings ) {
App app = new App(10);
App2 app2 = new App2(11);
App3 app3 = new App3(12);
System.out.println(“App’s number “ + app.number);
//prints it’s own number as well as it’s super’s number
app2. printNumbers();
app3. printNumbers();
}
}
It’s important to note because if you are making calls to get information from the super, you need to know that not one ur-super is made per program, that each child talks to. If you want fields or methods that are available, like an urtext, you need to make them static.
What 2 goals might make you want to provide an abstract class?
- You want to provide reusable methods and classes to developers in the parent class, yet force them to provide their own implementations.
- You don’t ever want someone to make an instance of that class, unless its a child.
What is an abstract class?
It’s a class marked with the abstract keyword that can’t be instantiated.
Can you have abstract methods on a non abstract class?
no
Will this compile?
public abstract class TestAbstract {
abstract public void printStuffMethod() {};
}
No. Abstract methods cannot have a method body as defined by the curly brackets.
Can you have non abstract methods on an abstract class?
Yes.
Can you have non abstract fields on an abstract class?
Yes
Can you have abstract methods inside a non abstract class?
no
Can you have abstract variables, methods, and classes?
No, you cannot have abstract variables. But you can have abstract methods and classes
Which of these wont compile:
public abstract class TestAbstract {
(1) public abstract int stuff;
(2) public int moreStuff;
(3) public void printStuffMethod() {};
(4) public abstract void printMoreStuffMethod() {};
(5) public void printEvenMoreStuff();
(6) public abstract void printEvenEvenMoreStuff();
}
1 won’t work because you can’t have an abstract field.
- this is just fine.
3 this is just fine
4 won’t work because abstract methods cant have a body
5 won’t work because it doesn’t have a body
6 this is fine
Does this compile?
public abstract class TestAbstract {
public int moreStuff;
public void printStuffMethod() {System.out.println(“in the abstract class”);};
public abstract void printMoreStuffMethod();
public abstract void printEvenEvenMoreStuff();
}
Yes. The int is fine because it’s not abstract. The method with a body is fine because its not abstract. The other abstract methods work because they don’t have a body.
Will this compile?
public abstract class whale{
protected abstract void sing();}
public class HumpbackWhale extends Whale{
private void sing(){System.out.println(“Humpback whale sings”);}
}
No. You cannot reduce the accessibility of a super’s method. In the super the method is protected, but in the child it is private.
When does an abstract class become useful?
When it is extended by a concrete subclass.
Does this compile?
public abstract class Animal{
public abstract String getName();}
public class Bird extends Animal{}
No. The first concrete class of an abstract subclass must implement the supers methods.
Why doesn’t this compile?
public abstract class Animal{
public abstract String getName();}
public class Walrus extends Animal{}
public abstract class Eagle extends Aniaml{}
This fails to compile because as Walrus is not abstract, and it is a first order descendent, it must implement the abstract methods of the parent. Because Eagle is abstract it compiles just fine.
Are abstract classes allowed to extend other abstract classes without implementing the super’s methods?
yes.
If an intermediate class (abstract OR concrete) provides an implementation for an abstract method, is that method inherited as a concrete method, or an abstract one? Does it matter whether or not the class providing the implementation is concrete or abstract?
It is inherited as a concrete method. It doesn’t matter whether the implementation comes from an abstract or concrete intermediary.
Assuming a concrete class extends this class, and that it is the first concrete class to extend it, which of the following methods must be implemented on the concrete child?
public abstract class TestAbstract {
public int moreStuff;
public void printStuffMethod() {System.out.println(“in the abstract class”);};
public abstract void printMoreStuffMethod();
public abstract void printEvenEvenMoreStuff();
}
All but the printStuffMethod(). As it already has an implementation, the child doesn’t need to provide a new one.
If this class were extended by an abstract class, which of the following methods would need to be overridden?
public abstract class TestAbstract {
public int moreStuff;
public void printStuffMethod() {System.out.println(“in the abstract class”);};
public abstract void printMoreStuffMethod();
public abstract void printEvenEvenMoreStuff();
}
None. Abstract classes don’t really have to do anything with the methods on their abstract super.