Inheritance COMP53 Sept 5 2007. Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.

Slides:



Advertisements
Similar presentations
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
© 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.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CS 2511 Fall UML Diagram Types  2 Main Types Structure Diagrams ○ Class Diagrams ○ Component Diagrams ○ Object Diagrams Behavior Diagrams ○
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
CPS Today’s topics Java Language Inheritance Upcoming Electric Circuits (not in text) Reading Great Ideas, Chapters 5.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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 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.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
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.
Systems Analysis and Design 8 th Edition Chapter 6 Object Modeling.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance comp249. Question 1 What type of inheritance does Java have? a) single inheritance b)double inheritance c) multiple inheritance d)class inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Object Oriented Programming: Inheritance Chapter 9.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Coming up: Inheritance
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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:
Peyman Dodangeh Sharif University of Technology Fall 2014.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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 Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
UML Class Diagram: class Rectangle
Class vs Abstract vs Interface
Polymorphism CT1513.
Pre-AP® Computer Science Quiz Key
Pre-AP® Computer Science Quiz
Advanced Programming Behnam Hatami Fall 2017.
Code reuse through subtyping
Java Programming, Second Edition
PreAP Computer Science Quiz
Presentation transcript:

Inheritance COMP53 Sept

Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members from the parent class Parent class = base class or super-class Child class = derived class or sub-class

Unified Modeling Language UML is a graphical language for designing classes Shape is the base class Circle and Rectangle are derived classes

UML class declarations show fields, methods and their modifiers UML Class Declaration protected fields public methods

A Circle has all the fields and methods defined by Shape, and some additional members Subclass Design

Another Subclass

We say that an object of a sub-class IS-A object of the super-class A Circle IS-A Shape A Rectangle IS-A Shape This means that a Circle or a Rectangle can be used anywhere that a Shape is expected. IS-A Relationship

Using Derived Class Objects // array of Shape references Shape[] shapes = new Shape[10]; // use a Circle as a Shape shapes[0] = new Circle(new Point2D(1,1), 10); // use a Rectangle as a Shape shapes[1] = new Rectangle(new Point2D(2,3), 10, 20);

Using Derived Class Objects // print area of a Circle System.out.println(shapes[0].getArea()); // print area of a Rectangle System.out.println(shapes[1].getArea()); Through dynamic dispatch, Java knows the true class of each object and can call the appropriate getArea() method for that class.