Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.

Slides:



Advertisements
Similar presentations
Reusable Classes.  Motivation: Write less code!
Advertisements

Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
Inheritance using Java
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Programming in Java CSCI-2220 Object Oriented Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
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.
Class Inheritance Part I
OOP: Encapsulation &Abstraction
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
One class is an extension of another.
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Extending Classes.
Java Programming Language
Advanced Java Programming
Overriding Methods & Class Hierarchies
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand on the shoulders of giants" Example: a program that extends class Frame from wheels – inherits the functionality of a Frame window with Quit button drawing of shapes – extends this functionality draws specific shapes

Inheritance Hierarchy in Java, a subclass has only one direct superclass – every class has one superclass only class Object has none – in other programming languages there can be several superclasses a subclass inherits all functionality – from its direct superclass from the superclass of its direct superclass, etc. – i.e. there is a chain of superclasses – the subclass inherits from all superclasses in the chain – classes form a tree, its root is the class Object a class declared without extends clause extends Object "is-a" relationship – an instance of a subclass also "is-a" an instance of its superclass

Modifiers define visibility of declared elements – i.e. where can they be used there are 4 ways: – public, private, protected, and no modifier public visible everywhere private visible only within the class where declared protected visible only within subclasses no modifier visible only within the package where declared

Inheritance: Methods and Overriding all methods in a superclass are inherited – except private methods – they can be directly called e.g., since toString() is defined in Object it can be called on any object subclasses can redefine methods declared in a superclass such method is called overidden subclass "knows better" how to "do it" e.g., we can override toString() it's a good idea since subclass knows better how to describe its objects watch out when you want to override a method! – overidden method must have exactly the same signature – otherwise it's a different method – e.g., tostring() doesn't override toString() lower case s – e.g., toString(String indent) doesn't override it, either different parameter list overriding is different from overloading!

Overriding and super what if we want to reuse the method from superclass? – e.g., while overriding toString(), we want to reuse toString() from superclass prefix call with super – e.g., public void toString() { return super.toString() + " more info"; } – this can be done in any method of the subclass we can only call the method being immediately overidden – e.g., super.super.toString() doesn't work!

Inheritance: Fields all fields in a superclass are inherited – they can be directly accessed if declared as public if declared as protected – private fields are still inherited, but not directly accessible what if subclass declares a field with the same name? – there are two such fields – subclass can access only the field it declares – it cannot access the field from superclass – prefixing with super doesn't work! – the field in is superclass is shadowed programming style: typically a shadowing a field is a mistake – the new field has a different purpose – so it should have different name

Subclass Constructor define a subclass using extends and name of the superclass – e.g. class Circle extends Ellipse {...} the constructor of the subclass must call a constructor of the superclass – this call must be the first statement – e.g. public Circle (Point at) { super (at); } we can leave out call to parameterless constructor of the superclass – Java "inserts" it – e.g. public Circle () { super (); //no need }

Polymorphism means many forms – a variable can reference objects of many types – a method can have many implementations variable declared using superclass can be assigned an instance of any subclass – even an indirect subclass e.g. variable declared as Object can be assigned any object! variable declared using interface type can be assigned an instance of any class that implements the interface type of the currently referenced object is the actual type when a method is called the implementation in actual type is executed – e.g. the overriden method is used!

Object-Oriented Programmig Object-Oriented Programmig (OOP) has 4 main concepts: objects and classes inheritance – subclasses extend functionality of superclasses encapsulation – visibility restrictions on accessing fields and methods polymorphism – variables hold objects of different types – actual types decides how variables behave

Abstract Classes a class can be declared as abstract – an abstract class can have abstract methods abstract method doesn't have a body – its declared like a method in an interface but an abstract method can be called! – e.g., abstract class Function { abstract double f (double x); void plot () { double y = f(x); } } abstract methods are implemented in subclasses – e.g., class Sin extends Function { double f (double x) {return Math.sin(x);} }

Use of Abstract Classes an abstract class cannot be instantiated – e.g. new Function(); won't work! but we can declare a variable using an abstract type and assign it an instance of a concrete subclass! – good example of polymorphism – e.g., Function f = new Sin(); f.plot(); f = new Cos(); //where Cos extends Function f.plot(); abstract classes are similar to interfaces – use interfaces if possible – use abstract classes if possible you need some methods that are implemented often you combine it to achieve maximum flexibility – an interface to declare behaviors – an abstract class to implement the general behaviors – concrete subclasses to implement specific behaviors