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

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Final and Abstract Classes
Introduction to Java 2 Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Object-Oriented PHP (1)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Object Oriented Programming: Inheritance Chapter 9.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Introduction to Object-oriented Programming
Object Oriented Programming: Inheritance
OOP: Encapsulation &Abstraction
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Analysis and Design
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
Inherited Classes in Java
Computer Programming with JAVA
Java Programming, Second Edition
Final and Abstract Classes
Extending Classes Through Inheritance
Presentation transcript:

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

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

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 {... }

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 {... }

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.

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

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

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

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.