Inheritance, Superclasses, Subclasses

Slides:



Advertisements
Similar presentations
School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
C HAPTER 7 Better Living in Objectville. O VERVIEW Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods.
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object Oriented Concepts. Movement toward Objects Instead of data-oriented or process-oriented Analysis, many firms are now moving to object-oriented.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Polymorphism & Interfaces
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Inheritance, Abstract & Interface Pepper With help from and
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!
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
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.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Ch. 2 Discussion Head First Java. Big Idea Differene between a class and an object What benefit object-oriented programming gives you. Read “Chair Wars”
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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”
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS 210 Introduction to OO Design August 24, 2006
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.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Tutorial 2 Shane Paul. Contact Details Shane Paul Rm: 378 Ext: 82766
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 (By Rama Bhadra Rao M) You can Trace me in Day 2
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Coming up A quick word about abstract How a class interfaces
Inheritance.
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Overriding Method.
One class is an extension of another.
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Week 4 Object-Oriented Programming (1): Inheritance
Recitation 6.
Chapter 10 Thinking in Objects
One class is an extension of another.
The Object-Oriented Thought Process Chapter 07
Advanced Programming Behnam Hatami Fall 2017.
Encapsulation Inheritance PolyMorhpism
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Abstract Classes Page
Java Programming, Second Edition
Interfaces and Abstract Classes
Chapter 9 Carrano Chapter 10 Small Java
Workshop for Programming And Systems Management Teachers
Advanced Inheritance Concepts
Inheritance and Polymorphism
Object Oriented Analysis and Design
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Extending Classes Through Inheritance
Presentation transcript:

Inheritance, Superclasses, Subclasses

Inheritance Allows for more usability of code Page 168 of Heads First book Class – SuperHero (Classes) Suits; tights; specialPower (Methods) useSpecialPower; putOnSuit Think of Parent/Child Relationship The sub-class Inherits from the SuperClass In Java, we say the subclass extends the SuperClass.

Overriding Methods You have the ability to override the method that are received from the parent. What you override is then specific to that subclass. SuperClass – class that is inherited from – more abstract Subclass – the specific “children” of the superclass.

Page 169 Example - Public class FamilyDoctor extends Doctor { Doctor – superclass Subclasses – Family Doctor, Surgeon Any subclass of Doctor will have a WorksAtHospital & a treatPatient property Extends – means “inherits from” CODE Public class FamilyDoctor extends Doctor { Can then put items specific to that class

Surgeon – page 169 Look at surgeon class TreatPatient method overrides the TreatPatient method in doctor class. A surgeon treats a patient by performing surgery, so that method overrides the method in Doctor class. Surgeon also has a MakeIncision method

Page 170 – Inheritance Tree Design Look for objects that have common attributes and behaviors Use inheritance to avoid duplicating code in subclasses. Design a class that represents the common state and behavior All Animals – have a picture; eat food, have hunger, etc.

Inheritance Tree, cont’d Decide if a subclass needs behaviors (method implementations) that are specific to that particular subclass type. MakeNoise and Eat are specific to each type of animal. Look for more opportunities to use abstraction, by finding two or more subclasses that might need common behavior.

Page 174 Finish the class Hierarchy Since animals already have an organizational hierarchy, that makes the most sense for class design. Felines – lion, tiger, cat (can use a common roam method) Canine – wolf, dog (can use a common roam method)