ch5 Flashcards
Where do all classes inherit from a single class called?
java.lang.object
public class Zoo extends java.lang.object{}
Does java.lang.Object have a parent class?
java.lang.Object is the only class that doesn’t have any
parent classes.
What are the rules?
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().
- The super() call may not be used after the first statement of the constructor.
- If no super() call is declared in a constructor, Java will insert a no-argument super()
as the first statement of the constructor. - 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. - 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);
}
}
~~~
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();
}
}
~~~
Primate
Ape
what is the difference between super() and super?
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
what are the checks performed by compiler when you override?
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.
What method cannot be overriden?
final method
How do you hide a variable?
When you hide a variable, you define a variable with the same name as a variable in a parent class
What is an abstract method?
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.
What can abstract class contain?
an abstract class may include nonabstract methods and variables.
Can an abstract method be declared in a regular class?
an abstract
method may only be defined in an abstract class.
Will the following compile
~~~
public abstract class Turtle {
public abstract void swim() {}
public abstract int getAge() {
return 10;
}
}
~~~
Needs to be the following :
public abstract void swim() {} ;
Has a body for getAge();
Can an abstract class be final?
No bc the sole purpose of abstract classes is to be implemented by another class
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”);
}
}
~~~
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”);
}
}
Make a abstract class?
public abstract class A{}
What keywords are automatically added to abstract methods?
public.
Can an abstract class be protected, private, default?
no only public. even if you dont put public, it will automatically add public.
not even static
What is inheritance?
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
Can a child access private memeber?
Finally, a child class may never access a private member of the parent class, at least not
through any direct reference
What does java support single? mutiple?
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.
Does java support multiple inheritance?
no diamond problem
can lead to complex, often diffi cult-to-maintain code.
What is the concrete class?
the first nonabstract susbclass that extends an abstract class and is required to implement all the inherited abstract method
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 {
}
~~~
public abstract class Animal { public abstract String getName(); } public class Walrus extends Animal { //does not compile } public abstract class Eagle extends Animal { //compiles }
- Because concrete classes need to implement the inherited methods
- abstract classes does not need to implement the inherited methods
What are the rules of abstract class definitions?
- Abstract classes cannot be instantiated directly
- It can be defined with any number, including zero of abstract and non-abstract methods
- They cannot be marked private or final, protected.
- An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods
- The first concrete class that extends an abstract class must provide an implementation for all the inherited abstract methods.
What are the rules of abstract method definitions?
- Abstract methods may only be defined in abstract classes.
- Abstract methods may not be declared private or final.
- Abstract methods must not provide a method body/implementation in the abstract
class for which is it declared. - 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.
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
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{
}
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
An interface ____ another interface?
extends
an abstract class _____ another abstract class
extends
What are the rules of interface variables?
- They are public, static and final
No private/Protected -> compile error - 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;
Make an interface code.
public abstract interface Can(){} //PUBLIC
abstract interface Cann(){} //Default
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
Can a class implement multiple interfaces?
A class may implement multiple interfaces
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()); } }
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();
}