C HAPTER 7 Better Living in Objectville. O VERVIEW Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Object Orientated Concepts. Overview  The world is objects  Defining an object  Not a function, it’s a method  Lets inherit want we can  Objects.
C HAPTER 8 Serious Polymorphism. O VERVIEW When to not instantiate Abstract Classes Abstract Methods Class Object Casting and Object reference Multiple.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
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.
© 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.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Polymorphism & Interfaces
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.
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.
Inheritance, Abstract & Interface Pepper With help from and
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
Week111 APCS-AB: Java Inheritance November 17, 2005.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
COMP Inheritance Basics Yi Hong June 09, 2015.
Coming up: Inheritance
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance ndex.html ndex.htmland “Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Inheritance & Polymorphism Android Club Agenda Inheritance Polymorphism.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance.
Inheritance, Polymorphism, and Interfaces. Oh My
Pre-AP® Computer Science Quiz
Advanced Programming Behnam Hatami Fall 2017.
Inheritance, Superclasses, Subclasses
Abstract Classes Page
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
PreAP Computer Science Quiz
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

C HAPTER 7 Better Living in Objectville

O VERVIEW Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods IS-A and HAS-A What is inherited from a superclass What does inheritance really help with Polymorphism Rules for Overriding Method Overloading

U NDERSTANDING I NHERITANCE A subclass inherits from a superclass –or- A subclass extends a superclass Class methods can be overridden but not instance variables

U NDERSTANDING I NHERITANCE Superclass Doctor Subclasses Family Doctor Surgeon

D ESIGNING AN I NHERITANCE T REE If you have multiple objects that need to inherit from other objects you need and inheritance tree Inheritance trees show which objects are sub classes to other objects They show what methods are overridden at each level

O VERRIDING M ETHODS When you have a class tree, the lowest method or most specific method is called first. wolf.roam calls from class Canine wolf.eat calls from class Wolf, not class Animal llama.eat calls from class Animal, not class Wolf

IS-A AND HAS-A Java lets us decide that anything can inherit from anything else Some Superclass/Subclass relationship do not make sense Use the IS-A and HAS-A test to decide Using the lists below, decide which items should be superclasses and which subclasses or which should be neither List1: Room, light switch, carpet, closet List2: Automobile, Wheel, Garage, Truck

W HAT IS INHERITED FROM A SUPERCLASS When a subclass inherits from a super class, it does not inherit private members Only public members are inherited

W HAT DOES INHERITANCE REALLY HELP WITH In summary the concept of inheritance: Allows for reducing duplicate code Allows for the definition of a common protocol for a group of objects protocol (def.): The established code of procedure or behavior in any group, organization, or situation. Polymorphism

P OLYMORPHISM Polymorphism lets you link a super class object reference to a subclass object. Before Polymorphism

M ORE P OLYMORPHISM Animal[] animals = new Animal[3]; animals[0] = new Dog(); animals[1] = new Cat(); animals[2] = new Lion();..... for(int i = 0; i<animals.length; i++){ animals[i].eat(); animals[i].roam(); }

M ORE P OLYMORPHISM class Vet { public void giveShot(Animal a){ // Give animal a shot a.makeNoise(); }

R ULES FOR O VERRIDING Overrides must have the same return types and arguments in the sub and super class to work. Overrides need to be as public or more in the subclass than in the superclass.

M ETHOD O VERLOADING Overloading lets us change the a function based on its inputs Powerful tool for handling different data types System.out.println() is an overloaded method System.out.println("String"); //Take a string System.out.println(5); //Take an int System.out.println('5'); //Take a character

A NOTHER EXAMPLE

T HINGS TO REMEMBER Inheritance is powerful but takes thought Using overriding makes using multiple object easier Using overloading gives more versatility to your methods