Introduction Flashcards
Java supports single an multilevel inheritance. What does that mean?
A class cannot extent from more than one class directly, but it can use a hierarchy
What is Polymorphism?
Polymorphism is the ability to process data differently depending on their types of inputs -> Same method name but with different parameters
What is dynamic Polymorphism?
Child class overrides the parent’s method
What is static Polymorphism?
Polymorphism which is resolved at compile time and thus does away with runtime virtual table lookups -> Multiple methods in one class have the same name
What are possible problems with Polymorphism?
Type identification during downcasting and fragile base class problem
When to use inheritance instead of composition?
Inheritance is used if classes fulfil the “is-a” condition and mainly provide additive functionality further down the class hierarchy
When to use composition instead of inheritance?
Composition allows to model objects that are made up of other objects -> the object(s) that compose or are contained by one object are destroyed too when that object is destroyed
What does the variable scope define?
The variable scope defines where variables can be seen
What method modifiers exist in java? And what is the default modifier?
public, protected, private, and package-private. Default is package-private
What is the problem with comparing two objects with the == operator?
Comparing two objects with the == operator returns false because it is not the values that get compared but the memory spaces -> Comparable and Comparator Interface
What are characteristics of abstract classes?
- Abstract modifier
- Abstract class can be subclassed, but it cannot be instantiated
- Abstract class has at least one abstract method but can declare both abstract and concrete methods
- A subclass derived from an abstract class must either implement ALL the abstract methods or be abstract itself
What are typical scenarios where we should prefer abstract classes over interfaces and concrete classes?
- Encapsulate some common functionality in one place that multiple subclasses will share
- Partially define an API that subclasses can easily extend and refine
- Subclasses need to inherit one or more common methods or fields with protected access
What is an interface?
An interface is an abstract type that contains a collection of method and constant variables
Why are interfaces used?
Interfaces are used to achieve abstraction, polymorphism and multiple inheritances
What are characteristics of an interface?
- We cannot instantiate interfaces directly
- Interfaces can be empty -> No variables or methods
- Interface methods cannot be protected or final
- Interface’s variables are public, static, and final by default and we are not allowed to change their visibility
What can we achieve by using interfaces?
- Behavioral Functionality
- Multiple Inheritances
- Polymorphism
What are the interface inheritance rules?
- Interface extending another interface -> All abstract methods are inherited
- Abstract class implementing an interface -> All abstract and default methods are inherited
What is the difference between a class and an interface?
- Concrete class is a blueprint for object creation
- Interface is a user-defined type which can have a collection of field constants and method signatures that will be overwritten by an implementing class