ch5 Flashcards
(92 cards)
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.