ch5 Flashcards

(92 cards)

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
What are the rules of abstract method definitions?
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.
26
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"); } }
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
27
What is an interface?
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{ }
28
What are the rules of an interface?
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
29
An interface ____ another interface?
extends
30
an abstract class _____ another abstract class
extends
31
What are the rules of interface variables?
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;
32
Make an interface code.
public abstract interface Can(){} //PUBLIC abstract interface Cann(){} //Default
33
what is a default method?
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
34
Can a class implement multiple interfaces?
A class may implement multiple interfaces
35
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()); } } ```
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()); } } ```
36
Can you do this? public interface CanFly { void fly(int speed); abstract void takeoff(); public abstract double dive(); }
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(); }
37
What is static method?
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
Does the following compile? Why? Why not? private final interface CanCrawl { private void dig(int depth); protected abstract double depth(); public final void surface(); }
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
If a static method is defined in an interface, how does inheritance work?
the classes that implements that interface will not be able to inherited.
40
Make a static method in the interface
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
What can you inherit from the parent class?
public and protected members of the parent class
42
What will compiler turn this into? public interface CanSwim { int MAXIMUM_DEPTH = 100; final static boolean UNDERWATER = true; public static final String TYPE = "Submersible"; }
public interface CanSwim { public static final int MAXIMUM_DEPTH = 100; public static final boolean UNDERWATER = true; public static final String TYPE = "Submersible"; }
43
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?
infinite loop do super.getAverageWeight();
44
Will the following compile? public interface CanDig { private int MAXIMUM_DEPTH = 100; protected abstract boolean UNDERWATER = false; public static String TYPE; }
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
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; } }
Yes because only public and protected methods are visible.
46
What is static method hiding?
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
Make a default method code ;
public default void hello(){ System.out.println("Some implementation"); }
48
What are the rules for method hiding?
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
All the methods of an interface has what access modifier?
public
50
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"); } }
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
Difference between method hiding and method overriding?
Method hiding is static and method overriding is non-static
52
Will this work? public class Bird { public final boolean hasFeathers() { return true; } } public class Penguin extends Bird { public final boolean hasFeathers() { return false; } }
no you cannot override final methods
53
Does Java allow variable overriding? Important
No.Java doesn’t allow variables to be overridden but instead hidden
54
What is polymorphism?
polymorphism -the property of an object to take on many different forms
55
Can you do the following? public class nilimaAssessment implements LuckyInterview extends Hardwork{ }
No public class nilimaAssessment extends Hardwork implements LuckyInterview{ }
56
Make an upcast
public class Parent(){ } public class Kid(){ } public class Main(){ public static void main(String[] args){ Parent a = new Kid(); } }
57
Make a downcast
Parent MrWilson = new Kid(); Kid alisson = (Kid) MrWilson; Parent MsWong = new Kid(); Kid Jimmy = (Kid) MsWong;
58
Can you do the following? Object o = new Object(); String s = (String) o;
No, gives you CLassCast Exception. Will compile // this will fail at runtime, because o doesn't reference a String
59
What is a virtual method?
A virtual method is a method in which the specific implementation is not determined until the runtime.
60
Give examples of virtual methods? Why?
non-final, non-static and non-private Java methods. They can be overridden at runtime.
61
What is polymorphic parameters?
The ability to pass instances of a subclass or interface to a method
62
a method that takes a parameter with type java.lang.Object
A method that takes a parameter with type java.lang.Object will take any reference. Including NULL
63
Both abstract and interfaces can be extended with what?
Both abstract classes and interfaces can be extended with the extends keyword
64
Where can static method be contained?
Both abstract classes and interfaces can contain static method
65
Where can default method be created?
Only interfaces.
66
Can concrete class implement what?
A concrete class must implement all inherited abstract methods. (Remember not interfaces)
67
When should i consider using abstract classes?
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
UML interface. A implements B A extends B
``` <> ------/> SuperClass _________/> sometimes interfaces are also circles ```
69
Can you make an abstract class the following? public default protected private
public = yes default = yes protected = no private= no
70
Methods in interfaces can be static?
yes.
71
Methods in interfaces can be protected
no
72
Interfaces methods are implicitly
public and abstract
73
Can you call a static method using a reference variable?
NO you need to use the Interfacename.staticmethodname();
74
``` nterface Jumpable{ int Min=10; } interface Move{ String Min="has cancer"; } public class Aa implements Jumpable, Move{ Aa(){ System.out.println(Min); } } ```
wont compile
75
# ``` interface PunchableFace{ void min(); } class CEO implements Punc{void min();}
DNC bc interface methods are public and you can trying to make it default. if you do default void min();
76
Can an interface define contructor?
No
77
Are static method ever inherited?
No
78
``` interface Jumpable{ int Min=10; } interface Move{ String Min="20"; } public class Aa implements Jumpable, Move{ } ```
will compile
79
``` interface Jumpable{ int Min=10; } interface Move{ String Min="cancer"; } public class Aa implements Jumpable, Move{ Aa(){ System.out.println(Min); } } ```
will not compile. Min is amb
80
``` 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; } } ```
Yes
81
``` interface Jumpable{ abstract String c(); } interface Move{ abstract void c(); } public class Aa implements Jumpable, Move{ @Override public String c() { return "C"; } } ```
dnc
82
# * Name all the method that can be defined in interface
abstract, default and static
83
Can you override default methods from an interface by a normal class?
no. only if thedefault return OBject and the subclass has lower or same
84
What happens when you modify static method to default/abstract?
the code that calls the method wont compile.
85
What happens when you modify abstract method to default/static?
abstract to default - continue to compile abstract method to static. wont
86
What happens from default to abstract? Static?
default to abstract - no default to static - no
87
``` 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"); } } ```
DNC, return types are different.
88
``` public interface H{ public int eatPlats(); } public interface O{ public void eatPlants(); } public interface Bear extends H, O } public abstract class Tiger implments O{} ```
DNC
89
Can you make the interface variable private, protected or abstract?
no it can only be public static final
90
interface A{ abstract int I(); } class Manager implements A{ int I(){ return 1; } }
if there is abstract, the concrete must do public.
91
``` public interface H{ static void eatPlats(); } public interface O{ static int eatPlants(); } public interface Bear extends H, O } ```
if static no problem.
92
interface Re{ void move(); } class I implements Re{ void move(); }
default method override. DNC make move public to compile