1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
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.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10: Inheritance and Polymorphism
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
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.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Computer Science I Inheritance Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Classes, Interfaces and Packages
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
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.
Modern Programming Tools And Techniques-I
Final and Abstract Classes
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Basics Programming with Inheritance
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
CS201: Data Structures and Discrete Mathematics I
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

1 Java Inheritance

2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner. Inheritance is more – it supports polymorphism at the language level

3 Inheritance Take an existing object type (collection of fields and methods) and extend it. –create a special version of the code without re- writing any of the existing code (or even explicitly calling it!). –End result is a more specific object type, called the sub-class / derived class / child class. –The original code is called the superclass / parent class / base class.

4 Inheritance Example Employee: name, , phone –FulltimeEmployee: also has salary, office, benefits, … Manager: CompanyCar, can change salaries, rates contracts, offices, etc. –Contractor: HourlyRate, ContractDuration, … A manager is a special kind of FullTimeEmployee, which is a special kind of Employee.

5 Polymorphism Create code that deals with general object types, without the need to know what specific type each object is. Generate a list of employee names: –all objects derived from Employee have a name field since Employee has a name field –no need to treat managers differently from anyone else.

6 Method Polymorphism The real power comes with methods/behaviors. A better example: –shape object types used by a drawing program. –we want to be able to handle any kind of shape someone wants to code (in the future). –we want to be able to write code now that can deal with shape objects (without knowing what they are!).

7 Shapes Shape: –color, layerfields –draw() draw itself on the screen –calcArea()calculates it's own area. –serialize()generate a string that can be saved and later used to re-generate the object.

8 Kinds of Shapes Rectangle Triangle Circle Each could be a kind of shape (could be specializations of the shape class). Each knows how to draw itself, etc. Could write code to have all shapes draw themselves, or save the whole collection to a file.

9 class definition class classname { field declarations { initialization code } Constructors Methods }

10 Abstract Class modifier Abstract modifier means that the class can be used as a superclass only. –no objects of this class can be created. –can have attributes, even code all are inherited methods can be overridden Used in inheritance hierarchies

11 Interesting Method Modifiers private/protected/public : –protected means private to all but subclasses –what if none of these specified? abstract : no implementation given, must be supplied by subclass. –the class itself must also be declared abstract final : the method cannot be changed by a subclass (no alternative implementation can be provided by a subclass).

12 Interesting Method Modifiers (that have nothing to do with this slide set) native : the method is written in some local code (C/C++) - the implementation is not provided in Java (recall assembler routines linked with C) synchronized : only one thread at a time can call the method (later)

13 Inheritance vs. Composition When one object type depends on another, the relationship could be: –is-a –has-a Sometimes it's hard to define the relationship, but in general you use composition (aggregation) when the relationship is has-a

14 Composition One class has instance variables that refer to object of another. Sometimes we have a collection of objects, the class just provides the glue. –establishes the relationship between objects. There is nothing special happening here (as far as the compiler is concerned).

15 Inheritance One object type is defined as being a special version of some other object type. –a specialization. The more general class is called: –base class, super class, parent class. The more specific class is called: –derived class, subclass, child class.

16 Inheritance A derived class object is an object of the base class. –is-a, not has-a. –all fields and methods are inherited. The derived class object also has some stuff that the base class does not provide (usually).

17 Java Inheritance Two kinds: –implementation: the code that defines methods. –interface: the method prototypes only. Other OOP languages often provide the same capabilities (but not as an explicit option).

18 Implementation Inheritance Derived class inherits the implementations of all methods from base class. –can replace some with alternatives. –new methods in derived class can access all non-private base class fields and methods. This is similar to (simple) C++ inheritance.

19 Accessing superclass methods from derived class. Can use super() to access all (non-private) superclass methods. –even those replaced with new versions in the derived class. Can use super() to call base class constructor. –use arguments to specify desired constructor

20 Single inheritance only (implementation inheritance). You can't extend more than one class! –the derived class can't have more than one base class. You can do multiple inheritance with interface inheritance.

21 Casting Objects A object of a derived class can be cast as an object of the base class. –this is much of the power! When a method is called, the selection of which version of method is run is totally dynamic. –overridden methods are dynamic. Note: Selection of overloaded methods is done at compile time. There are some situations in which this can cause confusion.

22 The class Object Granddaddy of all Java classes. All methods defined in the class Object are available in every class. Any object can be cast as an Object.

23 Interfaces An interface is a definition of method prototypes and possibly some constants (static final fields). An interface does not include the implementation of any methods, it just defines a set of methods that could be implemented.

24 interface implementation A class can implement an interface, this means that it provides implementations for all the methods in the interface. Java classes can implement any number of interfaces (multiple interface inheritance).

25 Interfaces can be extended Creation (definition) of interfaces can be done using inheritance: –one interface can extend another. Sometimes interfaces are used just as labeling mechanisms: –Look in the Java API documentation for interfaces like Cloneable. Example: BubbleSort w/ SortInterfaceDemo