Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
Inheritance Inheritance Reserved word protected Reserved word super
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
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.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Java Class and Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Chapter 10 Classes Continued
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
LECTURE 07 Programming using C# Inheritance
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
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.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
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 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
 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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance, Polymorphism, and Interfaces 1 What’s past is prologue. Don’t write it twice — write it once and reuse it. Advanced Placement Computer Science.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Advanced Programming in Java
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Classes and Inheritance
Interface, Subclass, and Abstract Class Review
Agenda Warmup AP Exam Review: Litvin A2
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 10 Thinking in Objects
CS Week 13 Jim Williams, PhD.
Overloading and Constructors
Java Inheritance.
MSIS 670 Object-Oriented Software Engineering
Advanced Programming Behnam Hatami Fall 2017.
Announcements & Review
Basics of OOP A class is the blueprint of an object.
CS 200 More Classes Jim Williams, PhD.
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

Inheritance

What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes inherit characteristics from other classes)

Example Taxonomy: The Animal Kingdom animal mammalinsect bearcatdogantcockroachtic

Example Taxonomy: Vehicles vehicle cartruck sedanvanwagonpickupsemitow bussuv

What Is Inheritance? Classes are organized in a hierarchy (subclasses and superclasses) A subclass inherits the attributes and behavior of its ancestor classes

Example: java.awt GUI Component Classes Component TextComponentMenuComponentContainer Button Canvas Checkbox CheckboxGroup Choice List Scrollbar TextAreaTextFieldMenuItemMenuBar Menu PopupMenu WindowPanel AppletDialogFrame FileDialog CheckboxMenuItem

Example: Circles and Wheels Develop classes to represent circles and wheels Users should be able to create a circle with a given color, corner point, width and height They should be able to move and draw a circle

Example: Circles and Wheels A wheel has all the attributes of a circle and a set of spokes in addition Users should be able to specify the number of spokes when instantiating a wheel Users should be able to change the number of spokes at any time

Class Diagram Wheel Circle Relationship is one of inheritance rather than aggregation, “is-a” rather than “has-a”

Class Diagram Wheel Circle All classes implicitly are descendants of Object, which lies at the root of the hierarchy Object

The Circle Interface public Circle() public Circle(int x, int y, int width, int height, Color c) public void draw(Graphics g) public void move(int xDistance, int yDistance) public boolean equals(Object other) public String toString()

The Wheel Interface public Wheel() public Wheel(int x, int y, int width, int height, Color c, int numSpokes) public void draw(Graphics g) public void move(int xDistance, int yDistance) public boolean equals(Object other) public String toString() public void setSpokes(int numSpokes)

Some Test Code import java.awt.*; import BreezySwing.*; public class ShapePanel extends GBPanel{ public ShapePanel(){ setBackground(Color.white); } public void paintComponent(Graphics g){ super.paintComponent(g); Circle c = new Circle(10, 10, 50, 50, Color.blue); Wheel w = new Wheel(100, 10, 50, 50, Color.red, 5); c.draw(g); w.draw(g); }

Some Test Code import BreezySwing.*; public class TestShapes extends GBFrame{ public TestShapes(){ addPanel(new ShapePanel(), 1,1,1,1); } public static void main(String[] args){ TestShapes theGUI = new TestShapes(); theGUI.setSize(300, 300); theGUI.setVisible(true); }

Extending a Class import java.awt.*; public class Circle { // Variables and methods } import java.awt.*; public class Wheel extends Circle { // Variables and methods }

Protected vs Private import java.awt.*; public class Circle { protected Color color; protected int x, y, width, height; } import java.awt.*; public class Wheel extends Circle { private int numSpokes; } Protected variables are visible in subclasses but not other clients

Circle Constructors import java.awt.*; public class Circle { protected Color color; protected int x, y, width, height; public Circle(int x, int y, int width, int height, Color c){ this.x = x; this.y = y; this.width = width; this.height = height; color = c; } public Circle(){ this(0, 0, 50, 50, Color.black); } this refers to the instance within its own class

Wheel Constructors import java.awt.*; public class Wheel extends Circle { private int numSpokes; public Wheel(int x, int y, int width, int height, Color c, int numSpokes){ super(x, y, width, height, c); this.numSpokes = numSpokes; } public Wheel(){ this(0, 0, 50, 50, Color.black, 5); } super refers to the instance as viewed by its parent class

Move the Shape import java.awt.*; public class Circle { protected Color color; protected int x, y, width, height; public void move(int xDistance, int yDistance){ x = x + xDistance; y = y + yDistance; } move is inherited by Wheel Each class manages its own data

toString import java.awt.*; public class Circle { public String toString(){ return "(" + x + "," + y + ")" + "\n" + "Width : " + width + "\n" + "Height: " + height + "\n " + "Color: " + color + "\n"; } import java.awt.*; public class Wheel extends Circle { public String toString(){ return super.toString() + "Spokes: " + numSpokes + "\n"; }

draw import java.awt.*; public class Circle { public void draw(Graphics g){ Color oldColor = g.getColor(); g.setColor(color); g.drawOval(x, y, width, height); g.setColor(oldColor); } import java.awt.*; public class Wheel extends Circle { public void draw(Graphics g){ super.draw(g); // The code for drawing the spokes would go here }

equals import java.awt.*; public class Circle { public boolean equals(Object other){ if (! (other instanceof Circle)) return false; else{ Circle c = (Circle) other; return x == c.x && y == c.y && width == c.width && height == c.height; }

equals import java.awt.*; public class Wheel extends Circle { public boolean equals(Object other){ return other instanceof Wheel && super.equals(other) && numSpokes == ((Wheel) other).numSpokes; }

Inheritance and Reuse Inheritance allows programs to reuse data and methods Eliminates redundancy and errors Try to place data and methods as high in the hierarchy as possible