Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

OOP: Inheritance By: Lamiaa Said.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance and Class Hierarchies Chapter 3 Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives Intro - Software OOP Inheritance, interfaces,
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.
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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
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.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Lists and the Collection Interface Review inheritance & collections.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
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.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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 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.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Data Structures and Algorithms
Extending Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College

Inheritance Encapsulation of code into classes makes reuse easier But many times we want to reuse “most of” a class. Inheritance allows information to be organized so that new classes can be derived from existing ones, instead of starting from scratch.

An Inheritance Hierarchy COMPUTER LAPTOP DESKTOP SERVER IBook Vaio Each object inherits data and methods from parent Goal: put information as close to root as possible superclass subclass

Inheritance Example Computer –Hard drive –Processor Laptop –Battery –(inherits Hard Drive, Processor from Computer) Server –Additional processors –(inherits Hard Drive, Processor from Computer)

Inheritance in Java All Java classes are arranged in a hierarchy –Extensive (e.g. Throwable sub-hierarchy) –Your objects will fit somewhere in this hierarchy Object is the superclass of all Java classes Subclasses refine or extend their superclasses –A laptop is a computer –A student is a person –A TA is a student

Is-a Versus Has-a Relationships IS-A –Denotes subclass relationship –E.g. Laptop IS-A computer –In Java, “extends” HAS-A –Denotes component relationship –E.g. Laptop HAS-A battery –In Java, one class is a data member of another class

Superclass and Subclass in UML Triangle-arrow denotes a subclass relationship The superclass is also called “base class”

Three Kinds of Object Visibility Public - accessible to any other class –Use for methods at the “interface” Private - accessible to no other class –Use for data members, as well as methods to be called only by the methods of this class Protected - accessible to subclasses (and classes in the same package) –Use when subclasses will need to access item, but external classes should not –Dangerous if package contains unrelated classes (or no package defined)

Constructing a Subclass When inherited objects are private, the subclass cannot access them to initialize! –This is normal and correct Use super() to explicitly construct the superclass first (with appropriate parameters) If not, super() (with no parameters) is implicitly called –If the superclass doesn’t have a default (no- parameter) constructor, this causes a compiler error.

Overriding Objects Parameter or local object overrides a data object in the class with the same name; use this. to specify the “hidden” object public void setName( String name ) { this.name = name; } Subclass’s method overrides parent’s method; use super. to specify the “hidden” method public String toString () { super.toString() + “ “ + battery; } Overridden methods must have the same return type

Method Overloading Method overloading: having multiple methods with the same name but different signatures (number and type of parameters) in a class Constructors are often overloaded Example: –MyClass(int inputA, int inputB) –MyClass(int inputA, int inputB, double inputC)

Polymorphism Polymorphism means many forms or many shapes In OOP, polymorphism refers to a single (superclass) object that can belong to one of many (subclass) classes Example: list of shapes –Shape[] myShapes = new Shape[5]; –myShapes[0] = new Square(5); –myShapes[1] = new Circle(2);

Which Method to Run? Consider: for (int j=0;j<2;j++){ System.out.println(Shapes[j].area()) } Compiler doesn’t know which area() method to run! Polymorphism allows the JVM to determine which method to invoke at run time –Even though it’s different for different iterations of the loop!

Abstract Classes An interface declares abstract methods –Contain name and parameters but no implementation An abstract class is between an interface and a (concrete) class –Can contain abstract methods (specified with keyword “abstract”) –Can also contain concrete methods and data objects Used for specifying polymorphic objects (e.g. Shape)

Classes, Abstract Classes and Interfaces

Abstract Classes and Interfaces Like an interface, an abstract class can’t be instantiated An abstract class can have constructors to initialize its data fields when a new subclass is created –Subclass uses super(…) to call the constructor May implement an interface but it doesn’t have to define all of the methods declared in the interface –Implementation is left to its subclasses