Unit 3 - Introduction to Inheritance - Example

Slides:



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

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Chapter 1 Inheritance University Of Ha’il.
Reusable Classes.  Motivation: Write less code!
OOP: Inheritance By: Lamiaa Said.
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
CSC 142 Computer Science II Zhen Jiang West Chester University
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
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.
Inheritance A Review of Objects, Classes, and Subclasses.
Classes and Objects Including inheritance. OOP — Object-Oriented Programming In OOP system, the class is a fundamental unit. An OOP program models a.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
 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.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Sections Inheritance and Abstract Classes
Inheritance, Abstract Classes, Interface and Polymorphism
OOP: Encapsulation &Abstraction
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Objects as a programming concept
Inheritance and Polymorphism
Lecture 10: Inheritance Subclasses and superclasses
C++ Classes C++ Interlude 1.
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
ITK 168 Lecture 5 - Inheritance
Inherited Classes in Java
Java Programming, Second Edition
Inheritance CT1513.
Law firm employee analogy
Inheritance.
An Example of Inheritance
Lecture 8 Inheritance CS140 Dick Steflik.
C++ Programming CLASS This pointer Static Class Friend Class
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Unit 3 - Introduction to Inheritance - Example

Inheritance In OOP a programmer can create a new class by extending an existing class Superclass (Base class) subclass extends superclass Subclass (Derived class)

A Subclass... inherits fields and methods of its superclass can add new fields and methods can redefine (override) a method of the superclass must provide its own constructors, but calls superclass’s constructors does not have direct access to its superclass’s private fields

public class Pacer extends Walker { public Pacer (int x, int y, Image leftPic, Image rightPic) super (x, y, leftPic, rightPic); } public void turnAround () Foot lf = getLeftFoot (); Foot rf = getRightFoot (); lf.turn (180); rf.turn (180); lf.moveSideways (-PIXELS_PER_INCH * 8); rf.moveSideways (PIXELS_PER_INCH * 8); Constructor Calls Walker’s constructor using super A new method Calls Walker’s accessor methods

Example Write a subclass of Walker called Bystander. Bystander should redefine (override) Walker’s firstStep, nextStep, and stop methods in such a way that Bystander alternates turning it’s left foot by 45 degrees left and right on subsequent steps but never moves the right foot. Bystander should also redefine the distanceTraveled method, to always return 0. To redefine a superclass’ method in a subclass, keep its header but change the code inside the { }. Define a new field which will help determine the direction of the left foot’s turn in each “step”. Do not duplicate methods inherited that remain the same.