Final Flashcards
What is inheritance?
When a new class is derived from an existing class
The superclass _______ the subclass
The superclass generalizes the subclass
In the UML diagram what is used to describe a sub/super class relation?
an arrow pointing from the sub class to the super class
What does the subclass inherit from the superclass?
The subclass inherits all methods and fields
What does Overriding mean?
The subclass can have its own version of the super class’s method. The subclass method must have the exact same signature.
Can use ‘@Override’ to avoid errors.
What is the Java syntax for Inheritance?
public class Shape{
protected String name;
}
public class Circle extends Shape{
private double radius;
}
Benefits of class inheritance?
One superclass can be reused by many subclasses, allows for DRY code.
Improves the structure of large programs(hierarchical).
Modular Programming(encapsulation).
Given a class called Circle that is a subclass of Shape.
if Shape has a method called draw() and Circle overrides that method, how would you reference the parent class overridden method?
super.draw() can be used to call the overridden parent method
Can a subclass inherit private fields/methods from a method?
No, private fields and methods are only accessible to the class it is declared in
Are the following subclasses the same?
a. public class A extends B{
}
b. public class A extends B{
Public A(){
super();
}
}
yes they are equivalent, in the first example Java will implicitly imply the super. keyword to invoke the parent constructor.
In the UML diagram what is used to reference generalization?
an Arrowhead that points to the Superclass
Are superclass constructors inherited?
No, superclass constructors are invoked either implicitly or explicitly.
Explicitly using the super() keyword
Who do these visibility modifiers grant access to?
a. Public
b. Private
c. none
d. Protected
a. All objects anywhere
b. Only objects of the same class
c. All objects of the same package
d. Only objects of the same subclass( or same package)
So from most visibility to least visibility is the following:
Public -> Protected -> none -> Private
Where does the super() keyword when being referenced in a child constructor?
Also given a parent class Person which has a protected field String name. Write the syntax for a constructor of the child class called Student with private int gpa.
The first line of the child constructor
public Student(String name, int gpa){
super(name)
this.gpa = gpa
}
What is the difference between single and multilevel inheritance?
single level inheritance is when methods/fields are being inherited from one level above
Multilevel inheritance is when methods/fields are inherited from non parent classes(parents of their parents)
Given the following class and constructor
public class A{
private int num;
public A(int num){
this.num = num;
}
}
Would a child class of this need to call the constructor?
public class B extends A{
public B(int num, String name){
super(num);
this.name = name;
}
}
Yes when the parent class doesn’t use the default constructor you must explicitly invoke the super() constructor.
True or False?
When a derived object is created
the “super portion”
defined in the superclass
must also be
created and initialized?
True
What is the Object class? Also provide all methods it provides
All classes in Java are subclasses of the Object class whether they extend it or not.
toString()
equals()
getClass()
hashCode()
finalize()
…
Note: these methods can be Overriden
True or False?
All objects in Java instantiate the Object class.
True
Applying the final modifier to a class, who can inherit it?
How about applying final to one of its methods instead?
When applying final to a class, that class can no longer be extended(inherited).
When applying final to a method that method can no longer be overridden.