• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/28

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

28 Cards in this Set

  • Front
  • Back
When can't methods be overriden?
1) When they are static.
2) When they are private in the superclass.
3) When they are final in the superclass.
4) When the method has default access in the superclass and the subclass is in a different package.
class A {
private void foo(){}
}
class B extends A {}

B tempB = new B();
tempB.foo();

Does this compile?
No. Because the method in class A is private, it is NOT INHERITTED IN B. Class B does not have a foo() method.
When might a class implement a single interface multiple times?
It is possible that 1 or more of its superclasses have declared the implementation. If the subclass then declares that it implements the same interface (legal) it does not have to implement the methods as it's already been done higher up in its class hierarchy.
When overriding a method, can you declare a return type that is a subclass of the return type in the overridden's method?
As of Java 5, yes. Before that the return type must be EXACTLY the same.
What happens when you return something from a method that has a 'void' return type?
Compiler error. You must never return anything from a method with void return type.
When making an object of a class with no declared constructors, only 1 constructor gets called in the making of the object.
True or False?
False. All of the superclasses in it's heirarchy also have their constructors called too.
Can an abstract class have a constructor? Does it have to have a constructor?
Yes and yes (kind of). Though you cannot instantiate an abstract class directly, it's constructor does get called when any descendent object is created (causing all it's ancestor constructors to be called including the one in our abstract class). It's perfectly acceptable to declare a constructor yourself in an abstract class. If you don't declare a constructor, as with non-abstract classes, the compiler will create one for you.
A default constructor is always public.
True or False?
False. A default constructor always has the same access modifier as it's class.
class Foo {
Foo() {}
}

What does the compiler always insert into every constructor if it's missing?
If a constructor body does not begin with a call to some version of this() or super(), the compiler will insert a call to the no-arg version of super() at the first line of the constructor.
What technique do you use to override a static method?
None. You can't override static methods. You can provide another static method with the same signature in a subclass but that called "hiding" (or a redefinition) - not an override. Redefinitions are bound at compile time, while overriding (inheritance) happens at runtime.
Class Dog{}
Class Beagle extends Dog{}

....
Dog d1 = new Dog();
Beagle b1 = new Beagle();
Beagle b2 = (Beagle) d1;

Does this compile? If not, what kind of exception is thrown?
Yes. It will throw an exception at runtime (ClassCastException) but it does compile. Compiler doesn't look at what object IS, it only considers what it COULD BE. A dog reference type MIGHT hold an object further down it's hierarchy (like a Beagle) so compiler has to trust user knows this when making this cast - and it allows.
(Y)x1.do();
((Y)x1).do();

Which is the correct cast?
((Y)x1).do();

Because the dot operator has presidence over the cast operator.
What causes loosely coupled classes?
Coupling is the degree to which 1 class knows about another class.

If A and B interact only through APIs offered by both and those APIs are limited, the classes are loosely coupled. If the innards of either change, as long as their limited APIs stay constant, neither is affected by such changes in the other (yeah)


If, on the other hand, A interacts not only through B's API but also uses non-API knowlege it has of B's innards, A is said to be TIGHTLY coupled B. If B's innards change, A is negatively impacted. (boo)
class A {
static void foo(){System.out.print(1);}
}
class B extends A {
void foo(){System.out.print(2);}
}

...
A obja = new B();
obja.foo();

What get's printed?
It fails to compile. You cannot declare a non-static method with the same signature as a static method in its own or any inherrited superclasses.
class A {
static void foo(){System.out.print(1);}
}
class B extends A {
static void foo(){System.out.print(2);}
}

...
A obja = new B();
obja.foo();

What get's printed?
1.

Static method calls are bound at compile time. That means the type of the reference determines what method gets called, not the type of the actual object.
Can you override a static method with an instance method of the same signature?
No. You can hide a static superclass method with a static method of the same signature in a subclass, though.
What are the steps of constructing object state
1. A constructor is called.

2. Member variables are initialized to their default values (at this point, the values can only be accessed using the new objects "this" reference, or for static variables, by using the class name).

3. The current constructor's first line will either call another local constructor( called the "chaining" of local constructors) or call explicitly or implicitly its parent via super(N number of arguments).

4. Calls to super(N) will chain all the way to the top of this object's ancestory hierarchy.

5. Immediately on return from a call to super, initializing expressions (any initiialization of member variables to non-default values) and initialization blocks are exectured in the order they are declared in the class.

6. The rest of the super(N) constructor is executed and returned from.

7. All nested calls to local constructors are completed and returned from.

NOTE: All calls to chained super constructors will also execute their own initialization expressions and blocks immediately upon return from calls to their own parents using super.
When is an object elligible for garbage collection?
When no LIVING THREAD can access it.
Consider 2 objects that each have references to the other inside themselves. Suppose then that we then set any other references to these objects to null. Each of these objects objects both still have 1 reference pointing to them -- the 1 from the other object. Are these 2 objects elligible for garbage collection?
Yes. Because a living thread can't get at them.
How do you request garbage collection?
Runtime rt = Runtime.getRuntime();
rt.gc();

OR

System.gc();
When does method hiding occur? How does it compare to method overriding?
Class methods (static) don't override each other, they hide each other - when they have the exact same signature.
class A {
static public void main (String[] args) {
A a1 = new B();
a1.foo();
}
static void foo(){System.out.println("A");}
}
class B extends A {
void foo(){System.out.println("B"); }
}

What prints?
Compiler error. You cannot attempt to override a static method with an instance method.
class A {
static public void main (String[] args) {
A a1 = new B();
a1.foo();
}
final static void foo(){System.out.println("A");}
}
class B extends A {
static void foo(){System.out.println("B");}
}

What prints?
Compiler error. If you declare a static method "final", then subclasses are not allowed to "hide" it. (static methods "hide" while non-private instance methods "override").
Does the following compile?

class A {
static void foo() {
System.out.println("A");
}
}

class B extends A {
static int foo() {
System.out.println("B");
return 1;
}
}
No.

If you declare in a subclass another static method with the same signature (method name + argument order and names) -- then this is considered a "hide" and the return type MUST be the same as the hidden static method.
Why doesn't this compile?

class A {
void foo() {}
}

class B extends A {
void foo() throws Error{}
}
It does. And runs just fine. Recognize that when a method is overrides another, it cannot thrown any new CHECKED exception that the superclass method didn't thrown. A subclass method can't throw more CHECKED exceptions than it's ancestor knows about.

BUT... an overriding method can declared that it throws ANY unchecked exception it wants to (though you don't need to declare these -- the point is you CAN declare non-checked exceptions and errors in a throws clause)
class A {
static void foo() { System.out.println("A");}
}

class B extends A {
static void foo() { System.out.println("B");}
}

class Temp {
public static void main(String[] args) {
A obja;
B objb = new B();
objb.foo();
obja = objb;
obja.foo();
}
}

What prints?
B
A

This compiles and runs without error.
class A {
void foo() { System.out.println("A");}
}

class B extends A {
void foo() { System.out.println("B");}
}

class Temp {
public static void main(String[] args) {
A obja;
B objb = new B();
objb.foo();
obja = objb;
obja.foo();
}
}

What prints?
B
B

Compiles and runs.
public class temp {
public static void main(String args[]) {
B obj = new B();
obj.foo(0, "");
}
}

class A{
void foo(int i, String s) {
System.out.println("A");
}
}
class B extends A {
void foo(String s, int i) {
System.out.println("B");
}
}


What prints?
This progam does compile and prints:
"A"

Notice that foo() is not overridden, and instead is overloaded.

One can change the order of the parameters of a method and that alone will cause the method to be treated like an overload.