Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_02 - 1 Vererbung (Inheritance)

Similar presentations


Presentation on theme: "Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_02 - 1 Vererbung (Inheritance)"— Presentation transcript:

1 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Vererbung (Inheritance)

2 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Inheritance Inheritance is that property of object-oriented programming that allows one class, called a subclass or derived class, to share the structure and behaviour of another class, called a superclass, or base class SuperClass / BaseClass SubClass / DerivedClass class extends

3 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Why Inheritance Inheritance allows you to "reuse the code" from a previous programming project without starting from scratch to reinvent the code Inheritance allows you to build a hierarchy among classes in terms of an IS-A relationship BankAccount CreditAccount GiroSavings isSubClassOf public class BankAccount {... } public class Giro extends BankAccount {... } public class CreditAccount extends Giro {... } public class Savings extends BankAccount {... }

4 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Declaring and Using Subclasses class extends { // subclass Data Members // subclass Method Members } Example: public class BankAccount {... } public class Giro extends BankAccount {... } public class CreditAccount extends Giro {... } public class Savings extends BankAccount {... }

5 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Why Inheritance The sub-class "inherits" all attributes and all methods form the super-class (cf. visibility). The inheritance works upwards. Attributes may be extended. Attention private attributes in the super-class are "features" of the sub-class but can not be accessed directly since they belong not to the class itself. But may be managed by constructors and Get/Set-methods. Methods may be extended and overridden. Method overloading is different than method overriding.

6 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Encapsulation, Information Hiding, Visibility

7 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Encapsulation and Information Hiding Encapsulation means to package data and/or operations into a single programming unit example: a record or table in a database program Information hiding means that there is a binding relationship between the information, or data, and its related operations outside the encapsulated unit cannot affect the information inside the unit. Visibility Levels: Classes Packages

8 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ package q; Encapsulation and Information Hiding a subclass in the same package as the superclass sees all non-private members a subclass in a different package as the superclass sees only protected and public members package p; public class C { private int x; public int y; int z; protected int i;... } public class D {... }... public class C1 extends p.C {... } accessible topublicprotectedpackage if nothing pecified private defining class class in the same package subclass in a different package non-subclass in a different package

9 Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Constructor chaining Constructor chaining to super-class class SuperClass { private int c; SuperClass() {// visibility of contructor is "package" this(10);// chaining } SuperClass(int c) { this.c = c; } class SubClass extends SuperClass { int a, b; SubClass(int a, int b, int c) { super(c);// c is an private attribute in the super-class; chaining this.a = a; this.b = b; } Empty super-class constructor is called automatically if no explicit constructor chaining is done. Consequently, always provide a meaningful empty constructor for your classes.


Download ppt "Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_02 - 1 Vererbung (Inheritance)"

Similar presentations


Ads by Google