Fundamentals of Software Development 1Slide 1 Inheritance Key ideas of Inheritance:Key ideas of Inheritance: –Overriding methods –Protected visibility.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Object Oriented Programming with Java
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CSSE221: Software Dev. Honors Day 6 Announcements Announcements Questions? Questions? Hint on BallWorlds’ changing the x and y coords in 2 nd half of lecture.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Abstract Classes and Interfaces
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Abstract classes and Interfaces. Abstract classes.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Intro to OOP with Java, C. Thomas Wu
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
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.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer 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.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Lecture 12 Inheritance.
Inheritance and Polymorphism
An Introduction to Inheritance
Class Inheritance (Cont.)
Interface.
Inherited Classes in Java
Object-Oriented Programming in PHP
Chapter 10: Method Overriding and method Overloading
Review of Previous Lesson
Method Overriding and method Overloading
More on Creating Classes part 3
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Fundamentals of Software Development 1Slide 1 Inheritance Key ideas of Inheritance:Key ideas of Inheritance: –Overriding methods –Protected visibility –The “super” keyword These extend yesterday’s ideas about constructors and the “this” keyword

Fundamentals of Software Development 1Slide 2 Inheritance Basics (a checklist to refer to) Classes extending other classes:* Only have to describe how they are different from parentsOnly have to describe how they are different from parents Don’t have to repeat methods which are “good enough” in the parent (say, getShape)Don’t have to repeat methods which are “good enough” in the parent (say, getShape) Can replace or “extend” any method which isn’t “good enough” (say, getColor)Can replace or “extend” any method which isn’t “good enough” (say, getColor) Can share fields with a parent in a “protected” way (say, this.xloc)Can share fields with a parent in a “protected” way (say, this.xloc) Only need to declare imports they use (like java.awt.Color)Only need to declare imports they use (like java.awt.Color) Don’t have to redeclare interfaces of the parent (like Animate)Don’t have to redeclare interfaces of the parent (like Animate) Require the parent to have a void-signature constructor!Require the parent to have a void-signature constructor! * Other than those extending abstract classes like “Ball,” as you did with “Dud”

Fundamentals of Software Development 1Slide 3 Overriding Methods DudThatMoves will “extend” DudDudThatMoves will “extend” Dud If it defines a constructor with the same signature, that overrides Dud’s constructorIf it defines a constructor with the same signature, that overrides Dud’s constructor If it defines a method with the same signature, that overrides Dud’s methodIf it defines a method with the same signature, that overrides Dud’s method What do you think happens if our child class doesn’t override a method in the parent class?What do you think happens if our child class doesn’t override a method in the parent class? It’s exactly the same as in the parent class!

Fundamentals of Software Development 1Slide 4 Overriding Methods Examples: “Dud” probably did nothing in the method“Dud” probably did nothing in the method public void act() { In “DudThatMoves” you simply define anotherIn “DudThatMoves” you simply define another public void act() { And it will override the one in Dud, doing what you want.And it will override the one in Dud, doing what you want.

Fundamentals of Software Development 1Slide 5 Protected Visibility These you can share between the parent class and its children.These you can share between the parent class and its children. Suppose in Dud you defined:Suppose in Dud you defined: protected double xloc; protected double yloc; Then these definitions would be “shared” just with its children (not the same actual copy of the fields), for whatever they wanted to use them for.Then these definitions would be “shared” just with its children (not the same actual copy of the fields), for whatever they wanted to use them for.

Fundamentals of Software Development 1Slide 6 Protected Visibility An exception to the “not the same fields” rule is if a field is static, say:An exception to the “not the same fields” rule is if a field is static, say: protected static int numberOfDuds; Then this same field is shared by all Duds and all their children, like DudsThatMove. But no other classes can access them.Then this same field is shared by all Duds and all their children, like DudsThatMove. But no other classes can access them.

Fundamentals of Software Development 1Slide 7 The “super” Keyword It’s like the word “this,” only “super”:It’s like the word “this,” only “super”: In a child class, “super” refers to its parent.In a child class, “super” refers to its parent. So, you can say,So, you can say, super(some parameter) Under the constructor for a child class, to call the constructor for the parent class (and whatever it does will be done here)Under the constructor for a child class, to call the constructor for the parent class (and whatever it does will be done here) Or you can do call super.method() to run a method of the parent class.Or you can do call super.method() to run a method of the parent class.

Fundamentals of Software Development 1Slide 8 The “super” Keyword If you want to do what the parent class does for a constructor or a method, and more, then:If you want to do what the parent class does for a constructor or a method, and more, then: Probably you want to call the “super” first,Probably you want to call the “super” first, Then do the additional things you’d planned.Then do the additional things you’d planned. My inherited method 1. Call the super 2. Do whatever else is needed