RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

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.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10: Inheritance and Polymorphism
© 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.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
What is inheritance? It is the ability to create a new class from an existing class.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
BY:- TOPS Technologies
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Java Programming Language
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept. Question from Review l Why do you have to cast an object (such as a String or Integer) being popped off of a stack and not an object (such as a String or Integer) being pushed onto the stack?

RIT Computer Science Dept. The Answer l A String is a type of Object l Is an Object a type of String? –No! This is why you have to tell the compiler what type of Object it’s dealing with –Note: You could ask the same about mammals and humans: A human is a type of mammal, but a mammal is not a type of human

RIT Computer Science Dept. Inheritance l Inheritance is a relationship where one class shares the structure or behaviors defined in another class

RIT Computer Science Dept. Why would you want inheritance? l Code re-use l Provides a way to specify some properties/behaviors that all subclasses must exhibit l It’s an appropriate way to represent the “is-a- kind-of” relationship between classes l Inheritance also provides the ability to generalize -- a method can be written to work with the super-class but subclasses can be passed as arguments

RIT Computer Science Dept. The Java Class Hierarchy l The root of the class hierarchy is the Object class l Every class (except the root) has exactly one parent l The parent is the (direct) superclass of the child l The children are (direct) subclasses of the parent.

RIT Computer Science Dept. Short Inheritance Questions l If class Tornado inherits from class Storm, mark the following statements as true or false: –There are more objects that conform to the type Tornado than there are that conform to the type Storm. –Tornado instances probably have more fields (attributes) than Storm instances, and definitely not fewer. –All methods declared in Storm can be called on instances of Tornado. –All methods declared in Storm will perform the same way whether they are invoked on instances of Storm or of Tornado.

RIT Computer Science Dept. Inheritance Examples l [See handout]

RIT Computer Science Dept. Write a class that uses inheritance l Using the superclass Goose given in class, write a subclass for your favorite type of Goose and the noise that your favorite Goose makes.

RIT Computer Science Dept. To Do l Name 5 classes that could be derived from Goose l Which is the correct assignment?: n Goose myGoose = new GrannyGoose(); n GrannyGoose myCat = new Goose();

RIT Computer Science Dept. Method Overriding l A subclass inherits everything from its superclass. l However, a subclass can override methods as shown in the GrannyGoose example [see handout] l A subclass can also add variables and methods

RIT Computer Science Dept. Comments about super() l super() will call the constructor for the superclass of the class it is called in l It must be the first call in a constructor (or else Java gives a syntax error) l If you do not call super() yourself, Java will automatically add the call at the beginning of the constructor l It’s better to call super() yourself rather than to depend on the default behavior that Java currently has

RIT Computer Science Dept. Why won’t this compile? public class TalkingGoose extends Goose { public TalkingGoose ( String name ) { super( name, true ); } // have this goose speak its name public void makeNoise ( ) { System.out.print( name ); } } // TalkingGoose

RIT Computer Science Dept. Access l Most classes provide three levels of access to their members (state and behavior): n Public: can be accessed by the methods in a class, in its subclasses, and by other classes. n Protected: can only be accessed by methods in the class, in its subclasses, and in classes in the same package. (Note: Wu doesn’t include the info. about packages – see page 668) n Private: can only be accessed by methods in the class.

RIT Computer Science Dept. Packages l Most classes are grouped into packages –For example, java.util is a package –Packages are like libraries of classes l If you don’t specify what package your class belongs to, it will belong to an unnamed default package

RIT Computer Science Dept. Abstract Classes l An abstract class is a class from which no instances can be generated. l Abstract classes can contain abstract methods: methods without implementation

RIT Computer Science Dept. Abstract Class Example l [See handout]

RIT Computer Science Dept. Polymorphism l In its simplest form, polymorphism allows a variable of type X to refer to any object that is an instance of X or an instance of a subclass of X.

RIT Computer Science Dept. Polymorphism (cont’d) l All classes are descendants of Object. So, a variable of type Object can refer to an instance of any class. l Using a Stack, this is why we can call push with a String argument, or with an Integer argument, or with a Rectangle argument, etc.

RIT Computer Science Dept. A Polymorphic Method Call class TestGoose3 { public static void main ( String[] args ) { Goose aGoose; GrannyGoose myGoose = new GrannyGoose( “Snookums” ); Object anotherGoose; WolfGoose wolfie = new WolfGoose( “Tigger” ); if ( Math.random() < 0.5 ) { aGoose = myGoose; } else { aGoose = wolfie; }

RIT Computer Science Dept. (cont’d) // The next line is a polymorphic method call: // Which method gets called depends on the type of // object that aGoose refers to. This will be // determined during run-time. aGoose.makeNoise(); anotherGoose = aGoose; // This line won’t compile: why? anotherGoose.makeNoise(); } } // TestGoose3

RIT Computer Science Dept. Yet more casting l This won’t compile: l This will compile, but will throw an exception/error: Goose aGoose = new WildCat( “Tigger” ); WolfGoose myGoose = aGoose; Goose aGoose = new GrannyGoose( “Selma” ); WolfGoose myGoose = (WolfGoose) aGoose;

RIT Computer Science Dept. Using Inheritance l Use inheritance to implement an “is-a” relationship –If A is a B, then make A a subclass of B –GrannyGoose is a type of Goose, so it is a subclass of Goose l Do not use inheritance to implement a “has-a” relationship –For example, a bird has feathers, a beak, toys, etc. These things should NOT be subclasses of Bird –This type of relationship is implemented by Bird containing an instance of Toy or Feathers and is called composition

RIT Computer Science Dept. How Final Works on Classes and Methods l A method may be declared as final. This means that the method cannot be overridden in a subclass. l A class may also be declared as final. Such a class cannot be subclassed.

RIT Computer Science Dept. Why final? l Why would you make a class final for security reasons?