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
What is a functional interface?
A functional interface is an interface that contains only one abstract method
When to use an interface?
- Using multiple inheritances and if there are different class hierarchies
- When unrelated classes implement the interface
- When application functionalities have to be defined as a contract, but not concerned about who implements the behavior
When to use an abstract class?
- When trying to provide a base class with methods that the subclasses override
- If we have specified requirements and only partial implementation details
- While classes that extend abstract classes have several common fields or methods (that require non-public modifiers)
- If one wants to have non-final or non-static methods to modify the states of an object
For what can the super keyword be used?
- Access the parent class
- Super() method can be used to call the parent default constructor
- Accessing parent class variables
- Accessing methods from the parent class
For what can the this keyword be used?
- Reference to the current object whose method is being called
- Disambiguating field shadowing
- Referencing constructors of the same class
- Returning this to return the current class instance
- Access the outer class and it’s variables from within the inner class
What is field shadowing?
Example: If constructor parameters have the same name as instance fields