Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
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.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
© 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 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
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.
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.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
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 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.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
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.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
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)
Classes Revisited Chapter 8.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance and Polymorphism
One class is an extension of another.
03/10/14 Inheritance-2.
Advanced Programming in Java
Week 8 Lecture -3 Inheritance and Polymorphism
One class is an extension of another.
Advanced Programming Behnam Hatami Fall 2017.
Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Inheritance

Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software design to take advantage of relationships, supporting reuse Supports the IS-A relationship –what’s the HAS-A relationship?

Terminology Base class/Parent class/Superclass –defines generic functionality Derived class/Child class/Subclass –extends or specializes base class

Syntax public class Student extends Person {…} public class Derived extends Base{…} public class Person {public class Student extends Person { … void print(); }

Subclasses Inherit members of parent May implement new members May override members of parent Person –name –Person(String) –print() –getName() Student –major –Student(String, String) –print() –changeMajor(String)

Method Invocation Person – print, getName Student – print, changeMajor Person p = new Person("Bob"); Student s = new Student("Alice", "CS"); s.getName(); p.print(); s.print(); p.changeMajor("Econ"); s.changeMajor("Econ");

Method Invocation Person – print, getName Student – print, changeMajor Person p = new Person("Bob"); Student s = new Student("Alice", "CS"); s.getName(); //Person getName p.print(); //Person print s.print(); //Student print p.changeMajor("Econ"); //ERROR s.changeMajor("Econ"); //Student changeMajor

Subclasses Person –name –Person(String) –print() –getName() Student –major –Student(String, String) –print() –changeMajor(String)

Protected private members of parent not accessible to child class protected members accessible only to derived classes examples public class Person { //will be accessible in Student protected String name; }

Partial Overriding //in Person class void print() {…} //in Student class void print() { super.print(); //super.methodName(); … }

More on super public Person(String name) { this.name = name; } public Student(String name, String major) { super(name); //super(params); this.major = major; }

Exercises 1.Implement and test the Person and Student classes. 1.What happens when you try to invoke changeMajor on a Person object?

Shadowing Variables Variable (same name) declared in base class and derived class Generally, a bad idea Example: Student declares a String variable name

final Classes and methods can be defined with final modifier –final public class Student … –final public void print()… final classes cannot be extended final methods cannot be overridden

abstract abstract classes cannot be instantiated Declare abstract class using abstract keyword –public abstract class Person … Method can also be abstract –public abstract void print() A class with an abstract method must be declared abstract

Polymorphism Many forms A variable is polymorphic if it can refer to different types of objects at different times Person p = new Person(“Bob”); p = new Student(“Sally”, “History”);

Example persondb Student Person[] persondb = new Person[10]; persondb[0] = new Student(“Sally”, “History”); persondb[1] = new Staff(…); … persondb[9] = new Faculty(…); Staff … Faculty

Dynamic Binding Determine which method is called based on object contents class Person { void print(); } class Student extends Person { void print(); }

Dynamic Binding Person p = new Person(…); Student s = new Student(…); p.print(); //calls Person print() p = s; //OKAY p.print(); //calls Student print() p.changeMajor(); //ERROR

Casting Person p; p = new Student(…); Student s = (Student)p; if(p instanceof Student) s = (Student)p; If cast is not successful, runtime error ClassCastException instanceof operator used to determine type

Example Class1Class2 extends Class1 1. f13. f2 2. f24. f3 Class1 c1 = new Class2(); Class2 c2 = new Class2(); c1.f3(); c1.f2(); c2 = (Class2)c1; c2.f3();

Exercises 1.Implement the Staff and Faculty classes discussed. 2.Create a PersonDB class that contains an array of Person objects. Implement an addPerson method that adds a new Person object to the array and a printStudents method that prints ONLY the Student objects stored in the array.

Object base class All classes derive from Object Defines several methods that can be overridden –String toString() –boolean equals(Object) –Object clone()