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.
How many methods does the class Lion need to implement?
public abstract class Animal{public abstract String getName();}
public abstract class BigCat extends Animal{public abstract void roar();}
public class Lion extends BigCat{}
Two. Because no implementation has previously been provided by the parent and the grandparent, Lion must do both.
Why doesn’t this compile?
abstract class Test2{
public abstract void printMoreStuff() {};
}
Can’t have a body {} on an abstract method.
Does this compile?
public class Main extends Test3 {
public static void main(String[] args) {
}
}
abstract class Test1{
public void printStuff() {System.out.println(“shiz”);}
}
abstract class Test2{
public abstract void printMoreStuff();
}
class Test3 extends Test2{
@Override
public void printMoreStuff() {
System.out.println(“Print More Stuff”);
}
}
yes
Why doesn’t this comopile?
public class Main extends Test3 {
public static void main(String[] args) {
}
}
abstract class Test1{
public void printStuff() {System.out.println(“shiz”);}
}
abstract class Test2{
public abstract void printMoreStuff();
}
class Test3 extends Test2{
}
Test 3 needs to implement printMoreStuff();
Why doesn’t this compile?
String name = “Jedd”;
switch (name){
case “Jedd”: System.out.println(“Name is equal to Jedd”);
case “Kade”: System.out.println(“Name is equal to Kade”);
case “Ben”: System.out.println(“Name is equal to Ben”);
case new String(“Tracy”) : System.out.println(“name is equal to Tracy”);
}
Case expressions must be constant expressions. So the ‘new’ keyword cant be used.
What is the output?
int x = 1;
switch (x){
case 1: System.out.println(“x is equal to one”);
case 2: System.out.println(“x is equal to 2”);
case 3: System.out.println(“x is equal to three”);
default: System.out.println(“none of these”);
}
as there is no break statement, everything after case 1, the case that returned true, will run.
What is the output?
int x = 5;
switch (x){
case 1: System.out.println(“x is equal to one”);
break;
case 2: System.out.println(“x is equal to 2”);
break;
case 3: System.out.println(“x is equal to three”);
break;
default: System.out.println(“none of these”);
case 4: System.out.println(“x is equal to 4”);
}
The default and case 4 will print out. Case 4 would not print out, however, if the default statement were followed by a break; statement.
Can you use a continue statement inside of a switch statment?
Yes. But it must be within a for loop of sorts.
can you make a List<t> of primitives? </t>
no, List<> can only work with objects.
what is the output:
char y = ‘y’;
char z = ‘z’;
List<integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);</integer>
switch (y) {
case ‘y’:
for (int i = 0; i < numbers.size(); i++) {
System.out.println(“y is equal to y”);
break;
}
break;
case ‘z’:
System.out.println(“y is equal to z”);
default:
System.out.println(“y isn’t equal to anything”);
}
}
y is equal to y
what is the output?
char y = ‘y’;
char z = ‘z’;
List<integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);</integer>
switch (y) {
case ‘y’:
for (int i = 0; i < numbers.size(); i++) {
System.out.println(“y is equal to y”);
continue;
}
break;
case ‘z’:
System.out.println(“y is equal to z”);
default:
System.out.println(“y isn’t equal to anything”);
}
y is equal to y
y is equal to y
y is equal to y
y is equal to y
y is equal to y
y is equal to y
y is equal to y
Can you instantiate an abstract class directly?
No.
How many abstract and/or non abstract methods can be defined on an abstract class?
As many as you want! Even zero
Can abstract classes be marked private, protected, or final?
NO
What happens to the abstract methods of an abstract class when it is extended by another abstract class?
It inherits all of the abstract methods as their own. So when the abstract child is extended by a concrete class, those methods are passed on even though they need not be defined on the abstract child.
What must the first concrete class which inherits abstract methods from an abstract parent?
Implement the abstract methods. Remember, however, that if the methods are not abstract and, therefore, have definitions, the concrete child doesn’t have to do anything with those already implemented methods.
Can you define an abstract method on a non-abstract class?
no
Can you declare abstract methods as private or final?
no
Can abstract methods have a body {}?
no
Is implementing an abstract method similar to overriding, in that the method signatures must match?
yes
How accessible does an implementation of an abstract method, or an overridden method, need to be?
At least as accessible as the method when it is first declared.
How many interfaces is a class allowed to implement?
Any number.
What is an interface?
An abstract data type that defines a list of abstract public methods that any class implementing the interface must provide.
Are interfaces allowed to include a list of constant variables and default methods?
yes
Why doesn’t this compile?
public interface TestInterface1 {
public int one = 1;
static int two = 2;
final int three = 3;
protected int four = 4;
}
only public, static, and final modifiers are allowed for interface constants.
Why doesn’t this compile?
public interface TestInterface1 {
public int one = 1;
static int two = 2;
final int three = 3;
public int four = one + 4;
public String name = “Jedd”;
public abstract int getNumberOne();
public int getNumberFour() {return this.four};
public default int getNumberTwo() {return TestInterface1.two;}
public default int getNumberThree() {return three;}
public static int getNumbers() {return one;}
}
The methods of an interface must have either static or default in the signature. Otherwise it wont compile. The rest are fine.
What part of an interface signature is assumed (not shown) in this example?
public interface Whatever{}
The abstract keyword.
Will this compile?
protected abstract interface Whatever{}
No. Protected is not allowed. All interfaces must be public.
Will this compile as the declaration of an interface?
interface Whatever{}
Yes. Public and abstract are both assumed for all interfaces.
What’s the proper way to define multiple implementations of interfaces on a concrete class?
public class Elephent implmements Interface1, Interface2, Interface3 and so on.
Can interfaces be instantiated directly?
no
Is an interface required to have any methods or fields?
no
Can an interface be marked as final?
no
Can an interface be marked as private?
no
What access is assumed for an interface?
public or default?
What level of concreteness is assumed for an interface?
abstract
Will making an interface private, protected, or final trigger a compiler error?
yes. But only if it’s a top level interface.
Which two method signature modifiers are all non-default methods on an interface assumed to have?
public abstract
What happens if you mark a method on an interface protected, private, or final?
This will trigger a compiler error.
Which asssumed keywords are we not seeing?
public interface Whatever{
void fly(int speed);
abstract void takeOff();
public abstract double dive();
public and abstract will be added to each method by the compiler.
Why doesn’t any of this compile?
private final interface CanCrawl{
private void dig(int depth);
protected abstract double depth();
public final void surface();
}
can’t mark an interface final or private.
cant mark a interface method private
can’t mark an interface method protected
can’t mark an interface method final
Which of the methods from this interface will need to be implemented by concrete class?
public interface TestInterface1 {
public int one = 1;
static int two = 2;
final int three = 3;
public int four = one + 4;
public String name = “Jedd”;
public abstract int getNumberOne();
public default int getNumberTwo() {return TestInterface1.two;}
public default int getNumberThree() {return three;}
public static int getNumbers() {return one;}
}
Only getNumberOne(); The others are default or static.
An interface that extends another interface, as well as an abstract class that implements an interface, ____ all of the abstract methods as its own abstract methods.
inherits
Does an interface implement another interface or extend it?
Extend
Does this compile?
public interface TestInterface3 extends TestInterface1, TestInterface2 {
}
yes
Can abstract classes extend multiple other abstract classes?
no
Can an interface extend more than one other interface? Why would you do that?
yes. To pass along the abstract methods to the child.
The first concrete class that implements an interface, or extends an abstract class that implements an interface, must _____
provide an implementation for all of the inherited abstract methods
What happens when an abstract class implements an interface?
The abstract class inherits the interface’s abstract methods but is not required to implement them.
Can a class extend an interface?
no
Will this compile?
public class Hello extends [interface]{
}
No. Only
Finish this sentence: class ____ interface {}
implements
Fill in the blank: interface ____ interface
extends
What happens if a class implements two interfaces, each of which has a method with the same signatures as that on the other interface? Will there be a collision when they are defined by the concrete class?
No. you only have to provide one implementation and it will count for both.
What happens when a concrete class implements two interfaces, each of which has methods of the same name with albeit different method signatures? Is the concrete class forced to provide to implementations, one for each method signature?
Yes. You must provide an implementation for each method signature override.
Is it possible in Java to define two methods in a class with the same name and input parameters but different return types? Why could this present a problem if the same method name has different return types, implemented from two different interfaces?
no. This is a porblem because you may think you are overriding a method, but you aren’t. The method name is the same but the signatures are not the same.
Interface variables are assumed to be ____, _____, and _____
public, static and final
The ____ of an interface variable must be ____ when it is declared since it is marked as _____ implicitly.
value, set, final
What is a default method?
A method defined within an interface with the default keyword in which a method body is provided
What’s the difference between an interface’s default method and it’s other “regular” methods?
The default can provide an implementation, which means it is not abstract.
Are classes required to override an interfaces default method?
No. But they can if they want to
Can a default method be declared in an abstract or concrete class?
No. Only in interfaces
Must the default method be marked with the default keyword?
yes
Can a method marked default not provide a body?
no
Is a default method assumed to be static, final, and abstract?
No. This is clear as it can be overridden by another class.
Like all methods in an interface, a default method is assumed to be _____ and will not compile if marked as private or protected
public
The type of the object determines which _____ exist within the object in memory.
properties
The type of the ______ to the object determines which methods and variables are accessible to the Java program.
reference
Why doesn’t this compile?
public class Main {
public static void main(Strings…strings) {
Harold harold = new Harold();
System.out.println(harold.getName());
Object object = harold;
System.out.println(object.getName());
}
}
class Harold{
protected String name = “Harold” ;
protected int age = 55;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Because once the reference object takes over the reference harold pointing at the Harold object, harold becomes only that which Object has the power to do.
What is the output?
public class Main {
public static void main(String…strings) {
Harold harold = new Harold();
Object objectHarold = harold;
Harold harold2 = (Harold) objectHarold;
harold2.setName(“ChangedName”);
System.out.println(objectHarold);
System.out.println(harold);
System.out.println(harold==objectHarold);
System.out.println(harold==harold2);
}
}
class Harold{
protected String name = “Harold” ;
protected int age = 55;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return getName();
}
}
ChangedName
ChangedName
true
true
Does casting an object from a subclass to a superclass require an explicit cast?
no
Does casting an object from a superclass to a subclass require an explicit cast?
yes
Will the compiler allow casts to unrelated types?
no
When casting an object type, is it possible for the code to compile without throwing an error during compile time, yet have an exception be thrown at runtime? If yes, why?
Yes. The error will be thrown at runtime of the object being cast is not actually an instance of that class.
What is a virtual method?
a method in which the specific implementation is not determined until run time.
In fact, all non-final, non-static, and non-private methods are considered ____ methods, since any of them can be overridden at runtime.
Virtual method
What is the output? And what is the principle demonstrated (hint: virtual methods)
public class Main {
public static void main(String… strings) {
Bird bird = new Peacock();
bird.displayInfo();
}
}
class Bird {
public String getName() {
return “Unknown”;
}
public void displayInfo() {
System.out.println(“The bird name is “ + getName());
}
}
class Peacock extends Bird{
public String getName() {
return “Peacock”;
}
}
The bird name is Peacock
Even though the Bird class defines a method getName(), a class extending Birdclass redefines a method of the same name, which returns a different String. This does not occur until runtime (aka when an instance of Peacock is created the method on the parent is redefined).
Explain a polymorphic parameter
A polymorphic parameter refers to the ability of subclasses to be passed as parameters to a method when the parameter defined is that of the super class. Because the superclass is the parameter type, you can pass in a subclass of the super as a parameter.
What is an exception?
Java’s way of saying i give up. I don’t know what to do, you need to fix something.
What do exceptions fundamentally do?
Interrupt program flow
Java has a ____ exception superclass for all objects.
Throwable
What are three subclasses of throwable?
java. lang._______
java. lang.________
(child of the preceding exception class)
java.lang_______
java. lang.Error
java. lang.Exception
java. lang.RuntimeException