\documentclass[a5paper]{handout} \csname endofdump\endcsname \usepackage{minted} \usetikzlibrary{decorations.pathreplacing} \geometry{top=0.75cm} \title{Vererbung} \begin{document} \thispagestyle{empty} \makedate \begin{defbox}{Vererbung} Eine Klasse kann \underline{alle} Attribute und Methoden einer anderen Klasse \pgap{\huge \boldred{erben}}. \\ \begin{tikzpicture} \begin{class}[text width=4cm]{Projektil}{0, 0} \end{class} \begin{class}[text width=4cm]{Laser}{6, 0} \iflehrer{\inherit{Projektil}}{} \end{class} \iflehrer{ \draw[decoration={brace}, decorate] (-2, 0.2) -- node[above=3pt] {Oberklasse} (2, 0.2); \draw[decoration={brace}, decorate] (4, 0.2) -- node[above=3pt] {Unterklasse} (8, 0.2); }{ \draw[opacity=0, decoration={brace}, decorate] (-2, 0.2) -- node[above=3pt] {Oberklasse} (2, 0.2); \draw[opacity=0, decoration={brace}, decorate] (4, 0.2) -- node[above=3pt] {Unterklasse} (8, 0.2); } \end{tikzpicture} \textbf{Man spricht:} \\ Die \pgap{\huge \textit{Unterklasse}} erbt von der \pgap{\huge \textit{Oberklasse}}. \\ Der Laser \pgap{\huge \textbf{ist}} ein Projektil. \end{defbox} \vspace{-0.5cm} \section*{Vererbung in Java} \vspace{-0.2cm} Das Schlüsselwort \pgap{\huge\texttt{extends}} in der Klassendefinition drückt eine Vererbungsbeziehung aus. \begin{tabular}{p{0.4\textwidth} p{0.6\textwidth}} \begin{minted}{java} class Oberklasse { int zahl; Oberklasse() { zahl = 13; } } \end{minted} & \begin{minted}{java} class Unterklasse extends Oberklasse { Unterklasse() { super(); zahl = 12; } } \end{minted} \end{tabular} \vspace{-1.25cm} \subsection*{Generalisierung} Gemeinsamkeiten mehreren Klassen in eine Oberklasse \textit{auszulagern} nennt man \pgap{\huge \boldred{Generalisierung}}. \\[0.1cm] \begin{tikzpicture} \begin{class}[text width=4cm]{Projektil}{0, 2} \iflehrer{\attribute{geschwindigkeit}}{\attribute{\phantom{\Huge geschwindigkeit}}} \iflehrer{\operation{bewegen()}}{\operation{\phantom{\Huge bewegen()}}} \end{class} \begin{class}[text width=4cm]{Laser}{7, 0} \iflehrer{\inherit{Projektil}}{} \end{class} \begin{class}[text width=4cm]{Lazer}{7, 2} \iflehrer{\inherit{Projektil}}{} \end{class} \end{tikzpicture} \end{document}