Inheritance INHERITANCE: extending classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

More on Classes Inheritance and Polymorphism
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
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.
Reusable Classes.  Motivation: Write less code!
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
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.
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
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.
Lecture 12 Inheritance.
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Java Inheritance.
Lecture 10: Inheritance Subclasses and superclasses
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Fundaments of Game Design
Chapter 11 Inheritance and Encapsulation and Polymorphism
Computer Science II for Majors
Presentation transcript:

Inheritance INHERITANCE: extending classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{ private String make; private String model; private int year; private double mileage; public Vehicle(String mk, String mdl, int yr, double mpg){ make = mk; model = mdl; year = yr; mileage = mpg; }

//class Vehicle continued public void setMake(String nmake) { make = nmake; } public void setModel(String nmodel) { model = nmodel; } public void setYear(int nyear) { year = nyear; } public void setMileage(double mpg) { mileage = mpg; }

//class Vehicle continued public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public String getMileage() { return mileage; } public String toString() { return make + “ “ + model+ “, Year: “ + year; }

And now we wanted a class Car. A car certainly needs all the data and methods we saw in the existing Vehicle class … plus more public class Car extends Vehicle{ } before we even write one declaration for class Car … All public (and protected) data and methods of Vehicle are automatically inherited !! (object of type Car will have all storage and access behaviors seen in Vehicle)

Vehicle class = superclass Car class = subclass In general, class SubclassName extends SuperclassName { instance fields methods }

Inheritance and Instance Fields Inherited data: All public and protected instance variables from the superclass are automatically inherited You can add supply additional instance data in an subclass that doesn’t exist in superclass Do not reuse instance data name in subclass (will hide super class data)

The class Car has inherited all instance data from the class Vehicle, but will need instance data of it’s own: public class Car extends Vehicle{ // data private boolean convertible; private double trunksize; // Car methods ………….. }

The class Car has inherited all methods from the class Vehicle, but will need methods of it’s own: public class Car extends Vehicle{ // data …….. // Car constructor public Car (String mk, String md, int yr, double mpg, boolean convert, double trusize){ super (mk,md,yr,mpg); //these locations are PRIVATE!! convertible = convert; trunksize = trusize; } ** call to superclass constructor MUST be first statement !!

//get and set public void setConvert(boolean conv){ convertible = conv; } public boolean getConvert() { return convertible; } //calculate distance that can currently be traveled public double distance (double gallons) { return gallons * getMileage(); //use superclass method to access // private data } //OVERRIDE the toString method public String toString() { return year + “ “ + model + “ trunk cap: “ + trunksize; }

Inherited Field Access we need to use constructor or set methods to change contents of instance data in Vehicle We need to use get methods to access data in data fields inherited from Vehicle ** This is because instance data in Vehicle was all declared as Private

Inheritance and Methods Override method: Supply a different implementation of a method that exists in the superclass Inherit method: Don't supply a new implementation of a method that exists in the superclass Add method: Supply a new method that doesn't exist in the superclass

Invoking a Superclass Method Subclass can directly call any public method (as we called getMileage above) *If subclass method has the same signature as a superclass method (say xxx) xxx() call within subclass calls subclass method super.xxx() call within subclass calls superclass method

Suppose we also need a class Truck : public class Truck extends Vehicle{ private double bedsize; private boolean towpackage; public Truck (String mk, String md, int yr, double mpg, boolean tow, double bsize){ super (mk,md,yr,mpg); //these locations are PRIVATE!! bedsize = bsize; towpackage = tow;; } //same concepts apply to Truck class as seen in Car class

//get and set public void setTowPackage(boolean tow){ towpackage = tow; } public boolean getTowPackage() { return towpackage; } //calculate how many of an item that can currently be carried public double quantity (int sizeofone) { return bedsize / sizeofone; }

Class Usage //can create and assign to same type reference Vehicle v1 = new Vehicle(“ford”,“mustang”,1966, 28.5); Car c1 = new Car(“vw”,”rabbit”, 1978, 35.2); //a subclass is the superclass type, but not vice versa Vehicle v2 = new Car(“cadillac”,”seville”, 1988, 16.0); Vehicle v3 = new Truck(“MAC”,”pickup”, 1968, 16.0); Car c2 = new Vehicle(“gmc”,”yukon”,122, 13.5); //error //public superclass methods can be called by subclass object v1.setMake(“Mercury”); c1.setMake(“Toyota”); v2.setMake(“Nissan”);

Class Usage cont. //public subclass methods cannot ALWAYS be called by super object vN.setConv(true); // only makes sense if vN is a Car //object, so compiler will object Car temp = (Car) vN; temp.setConv(true); //must cast to get by compiler if vN is NOT a Car the attempt to cast will throw a ClassCastException…….. use instanceof operator to check !!!!!!!

Class Usage cont. //subclass object ARE of superclass type (but not vice versa) Vehicle[] list = new Vehicle[10]; list[0] = c1; list[1] = t1; list[2] = v1; for (int i=0; i<2; i++) System.out.println(list[i]); WHICH toSTRING will be used??? (polymorphism again)

Access Control Level public - access provides to all classes Recommended way to work with objects == by calling their public methods private - access is restricted to inside the class Class instance variables should be kept private, ensuring their integrity and enforcing encapsulation model of object oriented programming protected -accessible by subclasses and package This is not recommended (except for efficiency purposes). As repeated inheritance occurs, access propagates and the benefits of information hiding is lost package access (the default, no modifier) Appropriate for package of classes which share data.

Inheritance Hierarchies Hierarchies of classes, subclasses, and sub- subclasses are common Example: Swing hierarchy Superclass JComponent has methods getWidth, getHeight AbstractButton class has methods to set/get button text and icon

A Part of the Hierarchy of Swing UI Components