CPSC 250 Advanced Java Flashcards
// Does this compile? If not, why?
import java.util.ArrayList;
public class Exam2 { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(new Parent()); list.add(new Child()); } }
class Parent { }
class Child extends Parent { }
It compiles. list.add(Parent x) expects a Parent. You can substitute a Child for a Parent any time.
“Every circle is a shape, but not every shape is a circle.”
// What is the difference between method1 and method2?
interface MyInterface { void method1(); public abstract void method2(); }
Nothing. All methods in interfaces are public and abstract.
It is implied by default, so you can leave out the keywords.
Writing interface methods as method1 is written is the best practice.
// Does this compile?
class Parent { Parent(int number) { } }
class Child extends Parent { }
// If not, why?
This one is complex.
It does not compile.
Parent supplies a constructor which take parameters (int), therefore the default constructor () is not supplied.
Since Child does not supply a constructor, the implicit default constructor is used.
The implicit default constructor calls the default constructor of the parent.
This fails because Parent did not define the default constructor().
// What gets printed? public class Exam2 { public static void main(String[] args) { Child c = new Child(); c.method(); } }
class Parent { public void method() { System.out.println("PARENT!"); } }
class Child extends Parent { public void method() { System.out.println("CHILD!"); } }
CHILD!
// Does this compile? abstract class MyClass { abstract void method1() {} void method2(); } // If not, why?
No, it does not compile. The {} and ; are mixed up.
// Does this compile? interface Interface { abstract void method(); } // If not, why?
Yes, it compiles, though it isn’t standard. All methods in an interface are abstract, therefore the abstract keyword is optional and is usually left out.
// Does this compile?
abstract class MyClass { void method1() { }
abstract void method2();
} // If not, why?
Yes, it compiles. You can have both abstract and normal methods in an abstract class.
// Does this compile?
abstract class Parent { abstract void method(); }
class Child extends Parent { void method() { } } // If not, why?
Yes, it compiles. Child implements all abstract methods of Parent.
// Does this compile?
abstract class MyClass { abstract void method(); } // If not, why not?
Yes! It compiles. Abstract method in an abstract class.
// Does this compile?
abstract class Parent { abstract void method(); }
class Child implements Parent { void method() { } } // If not, why?
No, it does not compile. Classes *extend* other classes (abstract or not) and *implement* interfaces.
// What gets printed? public class Exam2 { public static void main(String[] args) { Parent p = new Child(); p.method(); } } class Parent { public void method() { System.out.println("PARENT!"); } } class Child extends Parent { public void method() { System.out.println("CHILD!"); } }
CHILD! Even though p is a Parent, the object is a Child (new Child()). Java always calls overridden methods from the child class, not the parent. This is called dynamic resolution, or run-time binding.
// Does this compile?
class Parent { Parent(int number) { } }
class Child extends Parent { Child(int number) { } } // If not, why?
Child (int number) does not call Parent’s constructor as its first line, therefore super() is called by the compiler (implicit parent constructor call).
Since Parent() does not exist, it is an error.
Perhaps we could make this change:
Child (int number) {
super(number);
}
// Does this compile?
interface Parent { void method(); }
class Child implements Parent { private void method() { } } // If not, why?
Advanced Question.
No, it fails. You are not allowed to reduce the visibility of an inherited method. If it is public in the parent, it must be public in the child.
“method” is public in Parent because all methods in interfaces are public. Like “abstract”, it is implied.
// Does this compile?
class MyClass { MyClass() { super(); } } // If not, why?
It compiles.
With the absence of the “extend” keyword, you might suspect that super() is a mistake. But MyClass extends Object. Object is the parent of all classes by default! So super() call Object(), which is just fine. Even if you delete super(), it is called implicitly.
// Does MyClass have a constructor?
class MyClass { }
Tricky question.
You don’t *see* a constructor, but MyClass indeed *has* a constructor. The implicit default constructor is MyClass().
// Does this compile?
abstract class MyClass { abstract void method() {} } // If not, why?
No, it does not compile. Abstract methods must not have a {body}.