Shared Flashcard Set

Details

Java Cert - OCAJ-SE8 - Chapter 5 - Class Design
Flashcards for Chapter 5 on class design, inheritance, polymorphism, etc
90
Software
Professional
12/19/2017

Additional Software Flashcards

 


 

Cards

Term
Can a class that extends an abstract class or regular class, override a method that has a final modifier?
Definition
No, methods with the final modifier cannot be overridden under any circumstances, however if the abstract method were not final but private, then the extending class could hide the abstract method, by implementing its own method.
Term

Choose Reference or Object below.

 

Java determines which methods and variables are accessible to a program based on the reference/object.

Definition
by the type of reference used to access the object in memory.
Term
Choose between Reference and Object in the following statement.

The type of reference/object determines which properties exist in the object in memory.
Definition
Object
Term
Does a Java file have to contain a public class?
Definition
No
Term
Even though it is not possible to override a private method, since it is not accessible to child classes, you can _____ the private super class method with a new private method with the same or modified signature in the child class. Effectively the parent method is _____, and the child method is treated as a _____ and _____ method by the compiler.
Definition

1. redeclare

2. hidden

3. separate

4. independent

Term
Every class must have at least _____ contructor(s).
Definition
One
Term
How do you reference a parent method from a child class?
Definition
Using super. (super dot)
Term
How is Overloading a method different than Overriding a method?
Definition
In Overloading, a method has the same name, but will have different Input Parameters, whereas an Overridden method will have same signature (method name and input params) and the same or covariant return type.
Term

If a Child class hides a Parent's private instance method, and the reference to the Child object is via a Child reference, which method is invoked, the Parent or the Child?

Child c = new Child();

Definition
Child - the child hides the parents method, and the child's method gets invoked.
Term

If a child class hides a private parent instance method what happens when a Parent is used as a reference to a child - which method gets executed, the parent method or the child method?

Parent p = new Child();

Definition
Parent - since the reference is the Parent, it assumes that since the instance method is private, the Parent reference will access the Parent method.
Term
If a parent does not have a no-arg or default constructor, the compiler requires what in each child constructor?
Definition
An explicit call to an existing parent constructor.
Term
If return type is not void in an overridden method, the return type must be the ____ or a _____ which is called a _____.
Definition
same as parent return type
subclass of parent return type
covariant return type
Term
If you create a new static method in a child class that has the same signature and the same or covariant return type as the parent this is called method _____.
Definition
hiding
Term
Please fill in the blank with either a word or a phrase. regarding polymorphism and method overriding - an overridden method must be _____ as the method it is overriding.
Definition
at least as or more accessible
Term
Please list the 5 rules of Constructor Definition.
Definition

1. The first statement in every constructor is a call to another constructor in the class with the use of this(), OR

A call to a direct parent class using super().

2. The use of super() is not allowed after the first statement in a constructor.

3. If no super() is declared in a constructor, the compiler will insert a default no-arg super() call as the first statement.

4. If the parent doesn't have a no-arg constructor and the child does not define any constructors, the compiler with throw an error, and will try to insert a default no-arg constructor in the child class.

5. If the parent doesn't have a no-arg constructor, the compiler requires an explicit call to a parent constructor in each child constructor.

Term
The AM must be _____ as the parent AM.
Definition
at least as accessible
Term
The first statement in a constructor must be either _____, or _____.
Definition
this() or super()
Term
The first statement of every contructor is either a call to another constructor in the class using the keyword _____, or a call to a _____ constructor using the keyword _____.
Definition

1. this()

2. parent

3. super()

Term
True or false - you can declare default methods within abstract classes, regular classes and interfaces.
Definition
False. Default methods can only be declared in interfaces.
Term
True or false - Abstract classes cannot be instantiated directly.
Definition
True
Term
True or false - An abstract class can be marked as final.
Definition
False - this would mean that this class could never be extended.
Term
True or false - Interface variables must be set when they are declared?
Definition
True
Term
True or false - an abstract method can be marked as private.
Definition
False - you cannot tell a class that wants to extend an abstract class that all abstract methods must be implemented, but then make them inaccessible.
Term
True or false - an abstract method in an abstract class can be declared final.
Definition
False - this cannot happen because the method could never be overridden in the subclass
Term
True or false - casting from subclass to super class requires an explicit cast.
Definition
False. Going up the inheritance tree does not require an explicit cast.
Term
True or false - casting from super class to subclass requires an explicit cast.
Definition
True
Term
True or false - default methods in an interface can be static, final or abstract.
Definition
false. Default methods in an interface are not assumed to be static final or abstract because they must be allowed to be overridden.
Term
True or false - default methods in an interface will compile if Marked private or protected.
Definition
false – default methods in an interface are assumed to be public and will not compile if marked private or protected.
Term
True or false - it is ok in Java to define two methods in a class with the same signature but different return types.
Definition
False
Term
True or false - static interface methods are inherited.
Definition
False. Static interface methods are not inherited, and must be called using a reference to the interface.
Term
True or false - the compiler will not allow you to cast two unrelated types.
Definition
True.
Term
True or false - you can override a non-static method in a parent class when that method is marked as final.
Definition
False - you cannot override a non-static method in a parent class when that method is marked as final.
Term
True or false - you cannot hide a static method in a parent class when that static method is marked as final.
Definition
True - you cannot hide a static method in a parent class when that method is marked as final.
Term
What are the rules for abstract classes?
Definition

1. an abstract class cannot be instantiated directly

2. can have zero or more abstract and non-abstract methods

3. Cannot be marked as private, protected or final

4. an abstract class that extends another abstract class inherit all abstract methods as it own

5. The first concrete class must provide an implementation for all inherited abstract methods.

Term
What are the rules for abstract methods?
Definition

1.abstract methods can only be in abstract classes 2.abstract methods cannot be declared private or final 3.abstract methods can't provide a method body

4. when you implement abstract methods;

A. Name and signature must be the same

B. Visibility must be at least as or more accessible

Term
What are the rules for defining an interface?
Definition

1. interfaces cannot be instantiated directly.

2. An interface is not required to have any methods.

3. An interface may not be marked as private, protected or final (can only be public or pkg-private (default), assumed to be abstract)

4. Interfaces that extend other interfaces inherit all abstract interface methods as their own.

5. First concrete class that implements an interface OR the first concrete class that extends an abstract class that implements an interface, must implement all inherited abstract methods.

Term
What are the rules for interface abstract methods?
Definition

1.abstract interface methods are assumed public abstract.

2.abstract methods cannot be declared private, protected or final, non-default are assumed public abstract.

3.abstract interface methods can't provide a method body

4. when you implement abstract interface methods;

A. Name and signature must be the same

B. Visibility must be at least as or more accessible

Term
What are the rules for interface default methods?
Definition

1. A default method may only be declared in an interface and not a class or an abstract class.

 

2. A default method must be marked with the default key word. If a method is marked as default, it MUST provide a method body.

 

3. A default method is not assumed to be static or final or abstract, as it may be used or overridden in the class that implements the interface.

 

4. Like all methods in an interface, a default method is assumed to be public, and will not compile if marked private or protected.

Term
What are the rules for interface static methods?
Definition

1. Like all methods in an interface, static methods are assumed to be public and will not compile if marked as private or protected.

2. To reference a static interface method, a reference to the interface must be used.

Term
What checks does the compiler perform regarding a non-private method override?
Definition

1. Has the same signature as the parent method.

2. Overriding method must have at least the same or more accessibility as the parent method.

3. Does not have new or broader exception(s) than any exception in the parent class method.

4. Return value must be same as or a subclass (covariant) of the return type in the parent class.

Term
What happens when a parent class does not have a default no-arg constructor?
Definition
If there are other constructors in the parent, the compiler will not add a default no-arg consuctor. You must add a call to an existing parent constructor using super(T type).
Term
What happens when a parent does not have a default no-arg constructor and the child does not define ANY constructors?
Definition
The compiler will throw an error and try to insert a default no arg constructor.
Term
What happens when there is no constructor declared in a Java class?
Definition
The Java compiler will insert a default no-arg constructor.
Term
What happens when you define a variable with the same name as a variable in the parent?
Definition
It creates two copies within an instance.
Term
What is it called when a child class re-declares a static method from the parent class, and what are the rules for this?
Definition

hiding a static method

 

1. Child must have same signature.

 

2. Child must be at least as or more accessible.

 

3. Child method can't throw a new or broader exception.

 

4. Child return type must be same or subclass (covariant).

 

5. MUST be marked as static in child if it is marked as static in parent!

Term
What kind of classes can abstract methods be contained in?
Definition
Abstract Classes, and Interfaces are also allowed to contain abstract methods.
Term
What rules do the compiler follow when look at static method hiding?
Definition

Static method hiding has the same check rules as non-private, non-static method, except for the 5th one;

 

1. Has the same signature as the parent method.

2. Overriding method must have at least the same or more accessibility as the parent method.

3. Does not have new or broader exception(s) than any exception in the parent class method.

4. Return value must be same as or a subclass (covariant) of the return type in the parent class.

5. If marked as static in the parent, it MUST be marked as static in the child class (method hiding).Likewise, if parent method is NOT static, the child method CANNOT BE static (method overriding).

Term
When creating an overriding method, the new method must have the same _____ (which includes _____ and _____ ) and the same _____.
Definition

1. signature

2. method name

3. input parameters

4. return type or covariant return type

Term
When you create a new non-static method in a child class that is at least as or more accessible as a parent non-static method with same signature and covariant return type, this is called ____ a method.
Definition
Overriding
Term
When you create a new private non-static method in a child class that is at least as or more accessible as a parent private non-static method with same signature and covariant return type, this is called ____ a method.
Definition
re-declaring
Term
Where in a constructor and how often can a call to super() be made?
Definition
At the top, and only once.
Term
Which AMs are the only ones that can be applied to PACKAGE LEVEL CLASSES within a Java file?
Definition
public and pkg-private (default)
Term
Which Access Modifier (AM) means that members are not accessible by child classes or those classes in the same pkg?
Definition
private
Term
Within an interface, default methods must have a _____.
Definition
method body
Term
____ must be the first statement in the constructor.
Definition

super() to call a/the constructor in the super class, or

this() to call a constructor in the class itself

Term
can an interface that extends another interface re-declare a default method as abstract?
Definition
Yes. Interfaces that extend other interfaces, can also override default methods, or just inherit and pass along the default method implementation from the parent interface.
Term
explain the use of polymorphic parameters.
Definition
this is when you use super classes or interfaces as parameter types in methods.
Term
if no super() is declared as the _____ statement in a constructor, the compiler will insert a _____.
Definition
first,
no-arg super() statement.
Term
what are the assumed access modifiers for interface variables?
Definition
public static final
Term
what happens if a class implements 2 interfaces that have the same default method name with the same parameters?
Definition
it will not compile.
Term
what happens when a subclass implements multiple interfaces that have a default method with the same signature and the subclass overrides both default methods?
Definition
The subclass will compile and the compiler will use the overridden method in the subclass.
Term
what happens when an interface extends another interface or an abstract class implements an interface?
Definition
they inherit all abstract methods as their own.
Term
what happens when you have the same method name and parameters but different return types in multiple interfaces?
Definition
it is not possible in Java to have two methods with the same name and parameters but different return types - this will not compile.
Term
what is a virtual method?
Definition
A virtual method is one in which the specific implementation is not determined until run time.
Term
what is the exception rule when overriding a method?
Definition
A subclass cannot declare an overridden method with a new or broader exception class than exists in the super class.
Term
what must the first concrete class do that extends an abstract class or implements an interface?
Definition
provide an implementation for all inherited abstract methods.
Term
what type of methods are considered virtual methods and why?
Definition
all non-static, non-private, and non-final methods are considered virtual methods. This is because they can be overridden at anytime
Term
when overriding a method what is the rule about return types?
Definition
Must use same return type as parent or a covariant return type.
Term
why can't you Mark interface methods as private, protected or final?
Definition
because you will not be able to implement them, and all interface methods are assumed to be public.
Term
Exam will try to trick you by putting the "this()" call on the first line, but after another piece of code.
Definition

public MyClass(int i){

System.out.println(i); this();

}

 

Be careful.

Term
True or false - when you overload a method, you can even change it from instance to static.  The only thing that matters is having the same method name with different params (or different order of params).
Definition
True
Term
True or false - in an interface variables must all be public static final and MUST be initialized on declaration.
Definition
True
Term

True or false - if you have a class that implements multiple interfaces, you can not only cast the object to those interfaces, but if you cast to one of the implemented interfaces, java is smart enough to know that the underlying object has implemented them, and you can then cast the interface reference to another one, as long as you do an EXPLICIT cast and the object is not final;

i.e.

interface CanSwim{

 

}

interface HasFins{

 

}

public class Shark implements CanSwim, HasFins {

Object shark = new Shark();

CanSwim canSwim = (CanSwim) shark;

HasFins hasFins = (HasFins) canSwim;

}

Definition

True - here are the rules for casting to interfaces;

Lets say on the right side you have;

(DestinationType)sourceTypeReference (and DestinationType is an interface)

1) the type of sourceTypeReference is also an interface => cast is always allowed

2) the type of sourceTypeReference is a non-final class => cast is always allowed

3) the type of sourceTypeReference is a final class => cast is ONLY allowed allowed if the type of sourceTypeReference implements DestinationType; otherwise u get a compiler error

 

Keep in mind that 1) and 2) can throw ClassCastExceptions at runtime

Term
True or false - by explicitly casting to a subclass, you can gain access to methods and variables that were hidden from access.
Definition
True.
Term

True or false - in a method you can return the result of a call to a constructor;

i.e.

 

public in setTar(int z, int y, double d){

return this(z, y, d);

}

Definition
False - constructors do not return a value and have not return value in the declaration.
Term
True or false - when you implement an interface, you can update the value of an interface variable in the implementation code.
Definition
False - you cannot update the value of an interface variable, because all interface variables are public static final constants at the top level and cannot be updated!
Term
True or false - a class that implements an interace can only access interface fields by InterfaceName.theField.
Definition
False - for classes that implement interfaces, they can either call InterfaceName.theField, obj.theField or just refer to theField name itself.
Term
True or false - it is not legal to have an interface extend another interface and then declare the same abstract method.
Definition
False - it is ok to have ambiguous fields/methods, the definition to both resolves unambiguously to only 1 method.
Term
True or false - even in a superclass constructor that calls a parent method that has been overridden in the subclass, and a super() constructor call is made in the subclass constructor to that method, the overridden method is still bound by polymorphism, and the objects class drives which method is invoked.
Definition

True

i.e.

class A{

    A(){

        print();

    }

    Void print(){

        System.out.println("Hello");

    }

}

 

class B extends A{

    Int i = 4;

    Void print(){

        System.out.println(i);

    }

    public static void main(String[] args){

        A a = new B();

        

    }

 

}

In this case even though the print call exists in the parent constructor, because the method is an overriden non-static instance method, polymorphism takes over and calls the objects method from the particular class its instantiated from. The result would print 0, because we are still in A's constructor, and the int i variable in class B has not had a chance to be instantiated yet.

Term
True or false - a constructor can be static, final or abstract.
Definition
False
Term
True or false - interfaces cannot extend more than one interface.
Definition
FALSE! - Interfaces can indeed extend multiple interfaces, just need to separate by comma.
Term
True or false - in an Interface, you cannot override a default method with a static method. However you can have a default method in a sub-Interface with the same signature as the a static method in the super interface because a static method in an interface can only be called using the interfaces name.
Definition
True.
Term
True or false - a reference to a static field causes initialization of only the class or interface that declares that field (meaning the field is a part of the class or interface), even though it might be referred to through the name of a subclass, subinterface or a class that implements and interface.
Definition
True
Term
True or false - multiple inheritance if state only refers to the ability of a class to inherit instance fields from multiple classes (java does not allow this), multiple inheritance of implementation is the ability of a class to inherit instance methods from multiple classes(java supports this through default interface methods), and multiple inheritance of type includes the ability to implement multiple interfaces and/or the ability to extend from multiple classes (java supports this through the fact that an object can have multiple types: its own class and the types of all the interfaces the class implements).
Definition
True
Term
True or false - when narrowing objects in a hierarchy, casting is required when narrowing, but not on widening.
Definition
True
Term

True or false -  a constructor can take the same type as a parameter.

 

Test(Test t){

}

Definition
True
Term
True or false - when overriding and the return type in the original is a primitive the overriding method MUST also return a the same primitive type.
Definition
True
Term
True or false - the "this" reference is not available within a static method.
Definition
True
Term

True or false - You can call a static member in an Interface, from a reference variable.

 

public interface ITest {

public static void testMethod(){}

}

 

public class TestClass implements ITest{

public static void main(String[] args){

TestClass tc = new TestClass();

tc.testMethod();

}

}

Definition

False - you cannot call a static member in an Interface, you must call it via the Interface name.

 

ITest.testMethod();

Term

True or false - since member variables are hidden and not overridden, you can't access a member variable two levels up unless you cast it.

 

class c1 {

public static int i = 10;

}

 

class c2 extends c1 {

private static int i = 20;

}

 

class c3 extends c2 {

public static void main(String[] args){

c3 ceeThree = new c3();

 

System.out.println("int i="+ceeThree.i); // won't compile because c2's variable is private

 

System.out.println("int i="+((A)ceeThree).i); // will compile because c1's variable is public

}

}

 

Definition
True
Supporting users have an ad free experience!