ch5 Flashcards

1
Q

Where do all classes inherit from a single class called?

A

java.lang.object

public class Zoo extends java.lang.object{}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Does java.lang.Object have a parent class?

A

java.lang.Object is the only class that doesn’t have any
parent classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the rules?

A

1.The first statement of every constructor is a call to another constructor within the class
using this(), or a call to a constructor in the direct parent class using super().

  1. The super() call may not be used after the first statement of the constructor.
  2. If no super() call is declared in a constructor, Java will insert a no-argument super()
    as the first statement of the constructor.
  3. If the parent does have argument constructor and the child doesn’t define any
    constructors, the compiler will throw an error and try to insert a default no-argument
    constructor into the child class.
  4. If the parent does argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor.

Explicit call
~~~
public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal {
public Elephant() {
super(10);
}
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does it print?
~~~
class Primate {
public Primate() {
System.out.println(“Primate”);
}
}
class Ape extends Primate {
public Ape() {
System.out.println(“Ape”);
}
}
public class Chimpanzee extends Ape {
public static void main(String[] args) {
new Chimpanzee();
}
}
~~~

A

Primate
Ape

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the difference between super() and super?

A

super(), explicitly calls a parent constructor and may only be used in
the first line of a constructor of a child class.
The second, super, is a keyword used to reference a member defined in a parent class and may be used throughout the child class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what are the checks performed by compiler when you override?

A

The method in the child class must have the same signature as the method in the parent
class.

The method in the 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 a checked exception that is new or
broader than the class of any exception thrown in the parent class method.

If the method returns a value, it must be the same or a subclass of the method in the
parent class, known as covariant return types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What method cannot be overriden?

A

final method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you hide a variable?

A

When you hide a variable, you define a variable with the same name as a variable in a parent class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an abstract method?

A

An abstract method is a method marked with the abstract keyword defined in an
abstract class, for which no implementation is provided in the class in which it is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What can abstract class contain?

A

an abstract class may include nonabstract methods and variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can an abstract method be declared in a regular class?

A

an abstract
method may only be defined in an abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Will the following compile
~~~
public abstract class Turtle {
public abstract void swim() {}
public abstract int getAge() {
return 10;
}
}
~~~

A

Needs to be the following :
public abstract void swim() {} ;

Has a body for getAge();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can an abstract class be final?

A

No bc the sole purpose of abstract classes is to be implemented by another class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Does the following compile?
~~~
public abstract class Whale {
private abstract void sing();
}
public class HumpbackWhale extends Whale {
private void sing() {
System.out.println(“Humpback whale is singing”);
}
}
~~~

A

public abstract class Whale {
private abstract void sing(); this does not compile
}
public class HumpbackWhale extends Whale {
private void sing() {
System.out.println(“Humpback whale is singing”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Make a abstract class?

A
public abstract class A{}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What keywords are automatically added to abstract methods?

A

public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can an abstract class be protected, private, default?

A

no only public. even if you dont put public, it will automatically add public.
not even static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is inheritance?

A

Inheritance is the process by which the new child subclass automatically includes any
public or protected primitives, objects, or methods defined in the parent class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Can a child access private memeber?

A

Finally, a child class may never access a private member of the parent class, at least not
through any direct reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does java support single? mutiple?

A

Java supports single inheritance, by which a class may inherit from only one direct parent class. Java also supports multiple levels of inheritance, by which one class may extend
another class, which in turn extends another class. You can extend a class any number of
times, allowing each descendent to gain access to its ancestor’s members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Does java support multiple inheritance?

A

no diamond problem
can lead to complex, often diffi cult-to-maintain code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the concrete class?

A

the first nonabstract susbclass that extends an abstract class and is required to implement all the inherited abstract method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the output and why?
~~~
public abstract class Animal {
public abstract String getName();
}
public class Walrus extends Animal {
}
public abstract class Eagle extends Animal {
}
~~~

A
public abstract class Animal {
 public abstract String getName();
}
public class Walrus extends Animal { //does not compile
}
public abstract class Eagle extends Animal { //compiles
}
  1. Because concrete classes need to implement the inherited methods
  2. abstract classes does not need to implement the inherited methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are the rules of abstract class definitions?

A
  1. Abstract classes cannot be instantiated directly
  2. It can be defined with any number, including zero of abstract and non-abstract methods
  3. They cannot be marked private or final, protected.
  4. An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods
  5. The first concrete class that extends an abstract class must provide an implementation for all the inherited abstract methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the rules of abstract method definitions?

A
  1. Abstract methods may only be defined in abstract classes.
  2. Abstract methods may not be declared private or final.
  3. Abstract methods must not provide a method body/implementation in the abstract
    class for which is it declared.
  4. Implementing an abstract method in a subclass follows the same rules for overriding a method. For example, the name and signature must be the same, and the visibility of
    the method in the subclass must be at least as accessible as the method in the parent
    class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Will it compile?
public abstract class Whale {
protected abstract void sing();
}
public class HumpbackWhale extends Whale {
private void sing() {
System.out.println(“Humpback whale is singing”);
}
}

A

public abstract class Whale {
protected abstract void sing();
}
public class HumpbackWhale extends Whale {
private void sing() { // DOES NOT COMPILE
System.out.println(“Humpback whale is singing”);
}
}

No because the child is not accessible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is an interface?

A

An interface is an abstract data type that defines a list of abstract public methods that any class implementing the interface must provide.

public abstract interface CanBurrow{
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What are the rules of an interface?

A

It cannot be instantiated directly
Not required to have any methods
cannot be marked final
must have public/default access. anything else will give compile error
all methods in an interface need to have abstract/public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

An interface ____ another interface?

A

extends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

an abstract class _____ another abstract class

A

extends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are the rules of interface variables?

A
  1. They are public, static and final
    No private/Protected -> compile error
  2. if marked as final must be declared a value.

Even if you do
public int MAX = 90;
compiler will make it
public static final int MAX =90;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Make an interface code.

A

public abstract interface Can(){} //PUBLIC
abstract interface Cann(){} //Default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

what is a default method?

A

method with a default keyword.
1. may be only declared within an interface but not within a class/abstract class
2. marked with default keyword
3. not static, final or abstract (maybe overridden)
4. must be public. cannot be private/protected
5. should have implementation
6. cannot be static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Can a class implement multiple interfaces?

A

A class may implement multiple interfaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Why will this not compile? Is there a way to make it work?
~~~
public interface Walk {
public default int getSpeed() {
return 5;
}
}
public interface Run {
public default int getSpeed() {
return 10;
}
}
public class Cat implements Walk, Run { // DOES NOT COMPILE
public static void main(String[] args) {
System.out.println(new Cat().getSpeed());
}
}
~~~

A

it is not clear whether the code should output 5 or 10. The answer is that the code
outputs neither value—it fails to compile.
If a class implements two interfaces that have default methods with the same name and
signature, the compiler will throw an error

Yes override the method.

public class Cat implements Walk, Run {
 public int getSpeed() {
 return 1;
 }
 public static void main(String[] args) {
 System.out.println(new Cat().getSpeed());
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Can you do this?
public interface CanFly {
void fly(int speed);
abstract void takeoff();
public abstract double dive();
}

A

Compiler will automatically insert
1. public abstract
2. public abstract
3. public

public abstract interface CanFly {
public abstract void fly(int speed);
public abstract void takeoff();
public abstract double dive();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is static method?

A
  1. Like all methods in an interface, a static method is assumed to be public and will not
    compile if marked as private or protected.
  2. To reference the static method, a reference to the name of the interface must be used.

also
A static method defined in an interface
is not inherited in any classes that implement the interface

38
Q

Does the following compile? Why? Why not?
private final interface CanCrawl {
private void dig(int depth);
protected abstract double depth();
public final void surface();
}

A

private final interface CanCrawl { // DOES NOT COMPILE
private void dig(int depth); // DOES NOT COMPILE
protected abstract double depth(); // DOES NOT COMPILE
public final void surface(); // DOES NOT COMPILE
}

39
Q

If a static method is defined in an interface, how does inheritance work?

A

the classes that implements that interface will not be able to inherited.

40
Q

Make a static method in the interface

A

public abstract interface Lucky {
public static final String LUCK = “Luck is always in my favor”;
public static void lucky() {
System.out.println(LUCK);
System.out.println(“I will be super rich”);
}
}

public class Nilima implements Lucky{
public static void main(String[] args) {
Lucky.lucky();
Nilima.lucky();//cant do this.

} }
41
Q

What can you inherit from the parent class?

A

public and protected members of the parent class

42
Q

What will compiler turn this into?
public interface CanSwim {
int MAXIMUM_DEPTH = 100;
final static boolean UNDERWATER = true;
public static final String TYPE = “Submersible”;
}

A

public interface CanSwim {
public static final int MAXIMUM_DEPTH = 100;
public static final boolean UNDERWATER = true;
public static final String TYPE = “Submersible”;
}

43
Q

public class Canine {
public double getAverageWeight() {
return 50;
}
}
public class Wolf extends Canine {
public double getAverageWeight() {
return getAverageWeight()+20;
}
public static void main(String[] args) {
System.out.println(new Canine().getAverageWeight());
System.out.println(new Wolf().getAverageWeight());
}
}

what does the child return?

A

infinite loop

do
super.getAverageWeight();

44
Q

Will the following compile?
public interface CanDig {
private int MAXIMUM_DEPTH = 100;
protected abstract boolean UNDERWATER = false;
public static String TYPE;
}

A
  1. cant be private. Must be public
  2. Cannot be protected. Must be public
  3. Must assign a value
    you must provide a value to a static final member of the class when it is defi ned.
45
Q

Can you do the following?
public class Canine {
public double getAverageWeight() {
return 50;
}
}
public class Wolf extends Canine {
public double getAverageWeight() {
return super.getAverageWeight()+20;
}
public static void main(String[] args) {
System.out.println(new Canine().getAverageWeight());
System.out.println(new Wolf().getAverageWeight());
}
}

public class BactrianCamel extends Camel {
private int getNumberOfHumps() {
return 2;
}
}

A

Yes because only public and protected methods are visible.

46
Q

What is static method hiding?

A

A hidden method occurs when a child class defines a static method with the same name
and signature as a static method defined in a parent class.

47
Q

Make a default method code ;

A

public default void hello(){
System.out.println(“Some implementation”);
}

48
Q

What are the rules for method hiding?

A
  1. The method in the child class must have the same signature as the method in the parent
    class.
  2. The method in the child class must be at least as accessible or more accessible than the
    method in the parent class.
  3. The method in the child class may not throw a checked exception that is new or
    broader than the class of any exception thrown in the parent class method.
  4. If the method returns a value, it must be the same or a subclass of the method in the
    parent class, known as covariant return types.
  5. The method defined in the child class must be marked as static if it is marked as
    static in the parent class (method hiding). Likewise, the method must not be marked
    as static in t
49
Q

All the methods of an interface has what access modifier?

A

public

50
Q

public class Bear {
public static void sneeze() {
System.out.println(“Bear is sneezing”);
}
public void hibernate() {
System.out.println(“Bear is hibernating”);
}
}

public class Panda extends Bear {
public void sneeze() {
System.out.println(“Panda bear sneezes quietly”);
}
public static void hibernate() {
System.out.println(“Panda bear is going to sleep”);
}
}

A

does not compile because the child class is not method hiding correctly.
If the sneeze is static, the child method needs to be static as well.

51
Q

Difference between method hiding and method overriding?

A

Method hiding is static and method overriding is non-static

52
Q

Will this work?
public class Bird {
public final boolean hasFeathers() {
return true;
}
}
public class Penguin extends Bird {
public final boolean hasFeathers() {
return false;
}
}

A

no you cannot override final methods

53
Q

Does Java allow variable overriding? Important

A

No.Java doesn’t allow variables to be overridden but instead hidden

54
Q

What is polymorphism?

A

polymorphism -the property of an object to take on many different forms

55
Q

Can you do the following?
public class nilimaAssessment implements LuckyInterview extends Hardwork{

}

A

No
public class nilimaAssessment extends Hardwork implements LuckyInterview{

}

56
Q

Make an upcast

A

public class Parent(){
}
public class Kid(){
}
public class Main(){
public static void main(String[] args){
Parent a = new Kid();

}

}

57
Q

Make a downcast

A

Parent MrWilson = new Kid();
Kid alisson = (Kid) MrWilson;
Parent MsWong = new Kid();
Kid Jimmy = (Kid) MsWong;

58
Q

Can you do the following?
Object o = new Object();
String s = (String) o;

A

No, gives you CLassCast Exception.
Will compile
// this will fail at runtime, because o doesn’t reference a String

59
Q

What is a virtual method?

A

A virtual method is a method in which the specific implementation is not determined until the runtime.

60
Q

Give examples of virtual methods? Why?

A

non-final, non-static and non-private Java methods.
They can be overridden at runtime.

61
Q

What is polymorphic parameters?

A

The ability to pass instances of a subclass or interface to a method

62
Q

a method that takes a parameter with type java.lang.Object

A

A method that takes a parameter with type java.lang.Object will take any reference.
Including NULL

63
Q

Both abstract and interfaces can be extended with what?

A

Both abstract
classes and interfaces can be extended with the extends keyword

64
Q

Where can static method be contained?

A

Both
abstract classes and interfaces can contain static method

65
Q

Where can default method be created?

A

Only interfaces.

66
Q

Can concrete class implement what?

A

A concrete class must implement all inherited abstract methods.

(Remember not interfaces)

67
Q

When should i consider using abstract classes?

A

Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes.
You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

68
Q

UML interface. A implements B
A extends B

A
<<interface>> ------/>
SuperClass \_\_\_\_\_\_\_\_\_/>
sometimes interfaces are also circles
69
Q

Can you make an abstract class the following?
public
default
protected
private

A

public = yes
default = yes
protected = no
private= no

70
Q

Methods in interfaces can be static?

A

yes.

71
Q

Methods in interfaces can be protected

A

no

72
Q

Interfaces methods are implicitly

A

public and abstract

73
Q

Can you call a static method using a reference variable?

A

NO you need to use the Interfacename.staticmethodname();

74
Q
nterface Jumpable{
	int Min=10;
}
interface Move{
	String Min="has cancer";
}

public class Aa implements Jumpable, Move{
	Aa(){
		System.out.println(Min);

	}

}
A

wont compile

75
Q

```

interface PunchableFace{ void min();
}
class CEO implements Punc{void min();}

A

DNC bc interface methods are public and you can trying to make it default.

if you do default void min();

76
Q

Can an interface define contructor?

A

No

77
Q

Are static method ever inherited?

A

No

78
Q
interface Jumpable{
	int Min=10;
}
interface Move{
	String Min="20";
}

public class Aa implements Jumpable, Move{
	

}
A

will compile

79
Q
interface Jumpable{
	int Min=10;
}
interface Move{
	String Min="cancer";
}

public class Aa implements Jumpable, Move{
	Aa(){
		System.out.println(Min);

	}

}
A

will not compile. Min is amb

80
Q
interface Jumpable{
	abstract String c();
}
interface Move{
	abstract String c();
}

public class Aa implements Jumpable, Move{

	@Override
	public String c() {
		// TODO Auto-generated method stub
		return null;
	}

}
A

Yes

81
Q
interface Jumpable{
	abstract String c();
}
interface Move{
	abstract void c();
}

public class Aa implements Jumpable, Move{

	@Override
	public String c() {
		return "C";
		
	}

	

}
A

dnc

82
Q

*

Name all the method that can be defined in interface

A

abstract, default and static

83
Q

Can you override default methods from an interface by a normal class?

A

no. only if thedefault return OBject and the subclass has lower or same

84
Q

What happens when you modify static method to default/abstract?

A

the code that calls the method wont compile.

85
Q

What happens when you modify abstract method to default/static?

A

abstract to default - continue to compile
abstract method to static. wont

86
Q

What happens from default to abstract? Static?

A

default to abstract - no
default to static - no

87
Q
public interface H{
public int eatPlats();
}
public interface O{
public void eatPlants();
}

public class Bear implements H, O{
public int eatPlants(){
sysout("Eating":10);
return 10;
}
public void eatPlants(){
sysout("Eating");
}

}
A

DNC, return types are different.

88
Q
public interface H{
public int eatPlats();
}
public interface O{
public void eatPlants();
}

public interface Bear extends H, O
}
public abstract class Tiger implments O{}
A

DNC

89
Q

Can you make the interface variable private, protected or abstract?

A

no it can only be public static final

90
Q

interface A{
abstract int I();
}
class Manager implements A{
int I(){
return 1;
}
}

A

if there is abstract, the concrete must do public.

91
Q
public interface H{
static void eatPlats();
}
public interface O{
static int eatPlants();
}

public interface Bear extends H, O
}
A

if static no problem.

92
Q

interface Re{
void move();
}

class I implements Re{
void move();
}

A

default method override. DNC
make move public to compile