Software Construction: Inheritance

Cards to learn inheritance with

Cards to learn inheritance with


Fichier Détails

Cartes-fiches 16
Langue Deutsch
Catégorie Informatique
Niveau Université
Crée / Actualisé 04.01.2020 / 11.01.2020
Lien de web
https://card2brain.ch/cards/20200104_software_construction_inheritance
Intégrer
<iframe src="https://card2brain.ch/box/20200104_software_construction_inheritance/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

Class Diagram for Inheritance (superclass & subclasses)

The subclasses / children (e.g. MemorizingDeck & CircularDeck) inherit the methods from their superclass / parent (e.g. Deck).

The extends Keyword (Definition)

In java we extend / inherit a superclass / parent by using the extends keyword.

Defining a subclass (e.g., MemorizingDeck) as an extension of a superclass (e.g., Deck) means that when objects of the subclass are instantiated, the objects will be created by using the declaration of the subclass and of the declaration of the superclass.

Example 

public class MemorizingDeck extends Deck {
    private CardStack aDrawnCards;}

In java, can an objects type be chaged during run-time?

Downcasting (Definition)

If we instantiated a subclass with the superclasses type we can only use the methods contained in the superclass! To use the subclasses unique methods we need to downcast it with (SubclassType) object;.

Javas Single-Rooted Class Hierarchy (Definition)

Classes in Java are organized into a single-rooted class hierarchy. If a class does not declare to extend any class, by default it extends the library class Object.
Class Object constitutes the root of any class hierarchy in Java (All class instances directly or indirectly inherit from Object).

Example inheriting Fields

It will only create private final CardStack aDrawnCards = new CardStack(); (from MemorizingDeck) because aCards (from Deck) is private (and not public or protected!).

Only public or protected fields will be inherited!

(fields != methods)! (therefore methods from interface are not looked at here)

The field aCards can only be accessed if the superclass has setters / getters (as if it would be any other class).

What happens? Should work (???)

 

 

In what order are fields initialized with inheritance? (In java)

In Java the fields of an object are initialized “top down”, from the field declarations of the most general superclass down to the most specific class (the one named in the new statement).

Where does the subclass call the superclass?

The subclasses constructor automatically calls super() (the superclasses constructor).

Inheriting Methods in Java (Instance Methods)

An instance (i.e., non-static) method is just a different way to express a function that takes an object of its declaring class as its first argument.

Which implementation will be used? (A & B)

?IS THIS REALLY CORRECT?

A (First question): The implementation of MemorizingDeck.

B (Secondquestion): The implementation of Deck (no downcast).

Example of ambigious method use (How do we fix this?)

With super.draw() it is clear, that we want to call the method in the superclass.

Example of overridden method (How do we fix this?)

By annotating the overridden method with @Override.

Overloading Methods (Definition)

We can overload methods in a class by using the same method name but with different parameters. However: Avoid overloading methods except for widely used idioms (Commonly used patterns specific to a programming language).

The static type is considered for deciding the constructor!

Abstract Classes (Definition)

An abstract class is a superclass that is never instantiated itself (only indirectly by subclasses), comparable to an interface.

Example class diagram of Abstract Class

Abstract Class vs. Interface (in Java!)

  1. Interface methods are implicitly abstract & cannot have implementations while abstract classes can have instance methods when a subclass uses the default behavior.
  2. Variables in an interface are final by default while abstract classes can have non-final variables.
  3. Members of an interface are public by default while abstract classes can have public, privae & protected members.
  4. Interfaces are used with implements and abstract classes with extends.
  5. Interfaces can only implement another interface, while abstract classes can extend another abstract class & implement multiple interfaces.
  6. A class can implement multiple interfaces but it can extend only one abstract class.
  7. Interface is absolutely abstract and cannot be instantiated; An abstract class also cannot be instantiated, but can be invoked if a main() exists (???).
  8. In comparison with abstract classes, interfaces are slow as they require extra indirection.

Étudier