Recitation 6.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Object-oriented Programming Concepts
Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno.
Chapter 10 Classes Continued
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 Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Tutorial 5 Superclasses, Subclasses and Inheritance.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Lecture 12 March 16, The Scope of a Variable What if there are two variables with the same name? –A local or block-local variable can have the same.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Inspired by the Oulipu. The 3 Tenets of OO Encapsulation Polymorphism Inheritance.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
© 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.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Java Unit 11: Inheritance I
Programming in Java: lecture 7
Web Design & Development Lecture 9
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Object-Oriented Modeling
Advanced Java Topics Chapter 9
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Chapter 10 Object-Oriented Modeling
Types of Programming Languages
Road Map Inheritance Class hierarchy Overriding methods Constructors
Chapter 10 Thinking in Objects
Recitation 6 Inheritance.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Code reuse through subtyping
Sampath Kumar.S Assistant Professor/IT, SECE
Abstract Classes Page
Advanced Programming in Java
Java Programming, Second Edition
Inheritance.
Workshop for Programming And Systems Management Teachers
Inheritance and Polymorphism
Object Oriented Analysis and Design
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 11 Inheritance and Polymorphism Part 1
Object-Oriented Programming
Presentation transcript:

Recitation 6

Inheritance A way for a class to copy another class and build on it. “superclass” or “base class” “subclass” or “derived class”

Overriding methods You can also call your superclass methods, from any method inside the subclass:

Inheritance, cont. Inheritance is a way to reuse code. It is also a way to achieve polymorphism:

When should you use it? Conceptually, inheritance is meant to be used in “is a” relationships. For example, because “Apple is a Fruit”, Apple can inherit from Fruit. Putting it another way, Fruit is part of what makes an Apple an Apple. Another good measure of whether you should use inheritance: can the derived class be used anywhere that you need the base class? For example, if I have a function that needs a Fruit, an Apple works just as well (because “Apple is a Fruit”). Practically, inheritance is used when you need to make a class that is a more specific case of a different class. In this case, you probably need all the same code, with minor adjustments

How does it compare to interfaces? Practically speaking: Inheritance brings code with it, interfaces require you to write your own You can only inherit from one class in Java, but implement many interfaces However, both allow for polymorphism. Conceptually speaking: Interfaces help describe the role of a class instead of what it is An Apple is a Fruit, but has the roles of Food, Juiceable, RoundThing, etc Apple would inherit Fruit but implement the interfaces Food, Juiceable, etc. It’s a fuzzy distinction In general: If you’re looking just for polymorphism, interfaces are a better idea. Inheritance is used for code reuse, in cases where it is obvious that it is a good fit.