Lecture 11 and 12- Inheritance, Method overriding, Constructor, Object class Flashcards
What does inheritance stand for in OOP?
In OO languages, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
Java only supports single inheritance - each subclass can have one and only one direct super-class.
explain super class and subclass ?
Superclass or base, parent class Subclass or derived, child, extended class
What is the benefit of inheritance in OOP?
Code re-usability;
Better program structure.
What are the 4 areccess modifiers?
private
default
protected
public
Who can inherit by a private superclass?
any subclass whithin that superclass
What subclass can inherit from a default superclass?
Any subclass is in the same package as its parent, it also inherits the default (or package-private) attributes and methods of the parent.
When can a subclass inherit from a public or protected superclass?
A subclass inherits all of the public and protected attributes and methods of its parent as well as all its ancestors, no matter what package the subclass is in.
What can a subclass do apart from inheriting ?
add its own attributes and methods.
add same-signature methods - method overriding.
add same-name attributes (but not recommended) -
attribute hiding .
What is method overriding ?
A subclass defines a method which has the same name, number and type of parameters, and return type as a method in the superclass.
Recall Method overloading: If a class has multiple methods having the same name but different in parameters.
When overriding a method, it is recommended to use the @Override annotation, though it is optional.
What is a must for an overriding method ?
The access modifier for an overriding method can allow more, but not less, access than the overridden method.
Are constructors inherited ?
Constructors are not inherited. Each class defines its own constructors.
But the constructor of the super-class can be invoked from the subclass, using super().
When can the final keyword be used ?
A final attribute: it is a constant.
final int MAX BORROW BOOKS = 5;
A final method: we can not override this method.
final void eat(){…}
A final class: we can not extend it.
public final class String {...}//String class doesn’t have any subclasses
What is finally used for?
try-catch-finally
What is upcasting ?
Upcasting: assign a subclass instance to a superclass reference.
Animal d=new Dog(“black”,5,10); //upcasting
You can invoke all the methods defined in the Animal class
d. eat();//the superclass’s method
d. sleep();//the subclass’s version is invoked.
You CANNOT invoke methods defined in the Dog class for the reference d.
What is downcasting ?
Downcasting: revert a substituted instance back to a subclass reference.
What is commonly done to avoid class cast exception?
To avoid this ClassCastException, the common choice is to check the object’s class before downcasting by usin the instanceof operator.
if (d instanceof Dog) {
Dog d2 = (Dog)d;//downcasting safely
}
What is the instanceof operator ?
instanceof is an operator to verify if an object is an instance of a particular class.
What is the instanceof operator ?
instanceof is an operator to verify if an object is an instance of a particular class.
Dog d1 = new Dog("black",5,10); System.out.println(d1 instanceof Dog); // true System.out.println(d1 instanceof Animal); // true
What does binding refer to ?
Binding refers to the linking of a method call to its body (of an object type).
What are the two types of binding ? Explain them
Static/early binding:
When type of the object is determined at compile time(by the compiler).
Dynamic/late binding:
When type of the object is determined at runtime.
What is late binding?
When compiler is not able to resolve the call/binding at compile time, such binding is known as dynamic binding or late binding.
Overriden methods
Animal d=new Dog(“black”,5,10); //upcasting
d.sleep();//late binding
Or for example when compiling the Routine class, the compiler does not know which sleep() method will be called.
public class Routine { \_\_\_\_public static void aMethod(Animal a){ \_\_\_\_\_\_\_\_a.sleep(); \_\_\_\_} // ... other methods and attributes }
When does static binding happen ?
Methods cannot be overriden:
static methods
private methods
final methods
Overloaded methods void zoom(double factor){...} void zoom(double factorX, double factorY){...}
What is Polymorphism ?
Polymorphism means many forms in greek.
In OOP it is the mechanism of performing a single action in different ways.
In OOP languages, the polymorphism is implemented through method overriding (or late binding), i.e., the superclass defines a common method, its subclasses give different implementations by overriding it.
What is an abstract method ?
An abstract method: a method without an implementation.
abstract void getArea();
What is an abstract class?
An abstract class: a class that is declared abstract - it may or may not include abstract methods.
abstract class Shape{…}
Characteristics of abstract classes:
If one class has an abstract method, it must be declared as abstract.
Abstract classes cannot be instantiated. Animal a=new Animal("red",10);//compile error
If one class extends a class that has abstract methods, it must either provide the implementation of all abstract methods or be declared abstract as well.
What is abstraction ?
a concept of hiding all but the relevant data in order to reduce complexity and increase efficiency.
cases of abstraction?
Encapsulation is a kind of abstraction.
Abstract classes - achieve partial abstraction
Interfaces - achieve fully abstraction (100%)