Inheritance Chapter 9.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Inheritance Writing and using Classes effectively.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Inheritance Chapter 8.
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.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
CPSC150 Abstract Classes and Interfaces Chapter 10.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Abstract Classes and Interfaces
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
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.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Spring 2011.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
COMP Inheritance Basics Yi Hong June 09, 2015.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Inheritance and Polymorphism
Object-Oriented Programming: Polymorphism Chapter 10.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Java Unit 11: Inheritance I
Chapter 11 Inheritance and Polymorphism
12 Data abstraction Packages and encapsulation
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Polymorphism.
Inherited Classes in Java
Object Oriented Programming
Inheritance.
An Example of Inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance Chapter 9

Inheritance Superclass subclass of Animal superclass of dog and cat

Superclass has its own fields and methods Subclass inherits all of superclass fields and methods and adds some of its own Subclass has its own fields and methods

Inheritance Advantages One superclass for lots of subclasses Saves code rewriting for client Save code rewriting within class Note: There does not have to be an object of a superclass – there are no “animal” objects, but there may be: (triangle, isosceles triangle, right angle isosceles triangle).

Shapes Example Circle has: Triangle has: Make a parent: Shape color, xPosition, yPosition, Diameter Triangle has: color, xPosition, yPosition, length, height Make a parent: Shape

Assignment Circle c = new Circle ( ); Shape s = new Shape( ); s = c; // ok, a circle IS a shape c = s; // NOT ok. A shape might be something other than a Circle

Assignment a = b; // all b are a b must the same class as a, or a subclass of a b IS-A a (note: backwards of assignment order) dog is a mammal Circle is a Shape does NOT work for a IS-A b (same order as assignment) mammal is not necessarily a dog

Objects & methods obj.method( ) method must be in obj class or a parent class Circle c = new Circle(); c.draw( ); // ok. draw is a Circle method c.changeColor("green"); // ok. changeColor is a Shape method Shape s; s.draw( ) ; // NOT ok. draw is a method of its children s.changeColor("green"); // ok. changeColor is a Shape method If method is in the super class, ok. that's the point of inheritance.

Subtyping Object variables in Java can hold objects of the declared type, or of subtypes of the declared type. Big advantage in many applications

Subtyping Shape s1 = new Shape( ); // ok Circle c = new Circle( ); // ok Triangle t = new Triangle( ); // ok Shape s2 = new Circle( ); //ok. all circles are shapes Circle c2 = new Shape( ); // NOT ok. not all shapes // are circles --------- c.findCircumference ( ); // ok if method exists s2.findCircumference( ); // NOT ok ((Circle)s2).findCircumference( ); // ok s2 is a Circle LHS must be same level or higher than RHS object must be same level or lower than method Casting

Casting Turn one type into another (when it’s ok) double sphere = 4 / 3 * Math.PI * r * r * r; fix: double sphere = (double) 4 / 3 * Math.PI * r * r * r; 4.0 / 3 = double/ int = double / double = 1.333… 4 / 3 = 1 Creates a new value, 4.0 (double) from 4 3 is automatically coerced into a double. Casting does it explicitly, and is programmer controlled

Casting and Inheritance Practice A. Ok B. Not OK int y=3; double g=3; int x = (double) y; double z = (int) g; MyShape s = new Circle( ); Circle c = new Circle( ); s.moveVertical(10); c.moveVertical(10); c.findCircumference( ); s.findCircumference( ); ((Circle) s).findCircumference( ); critical } Also critical }

Methods and Inheritance ok to call method in the class or in a superclass NOT ok to call method of a subclass To do that, object must be cast to the class where method appears ((Circle) s).draw( ); // no syntax error // ok if s is a Circle Will get a run-time error if object is NOT the subclass Shape s = new Square( ) ; // ok ((Circle) s).draw( ); // ok in syntax. run time error

Practice Dog d = new Mammal(); Mammal m = new Dog( ); private final int nbrlegs = 4; private final stuff covering = FUR; ----------- Mammal() { // constructor} FeedsYoung() { nurses();} GivesBirth() { liveyoung(); } Practice A. OK B. NOT OK Dog d = new Mammal(); Mammal m = new Dog( ); StBernard s = new Dog(); m.FeedsYoung( ); d.slobbers(); s.wagstail(); s.FeedsYoung(); Dog // fields for dogs ------- Dog() {// constructor } public void wagstail() { } StBernard // fields for St. Bernards ------ StBernard { /* constructor*/} public void slobbers() { }

Practice: A. OK B. Not OK Assume we have 4 classes: Person, Teacher, Student and PhDStudent. Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student. Which of the following are legal? Person p1 = new Student( ); Person p2 = new PhDStudent( ); PhDStudent phd1 = new Student ( ); Teacher t1 = new Person( ); Student s1 = new PhDStudent( ); s1 = p1; s1 = p2; p1 = s1; t1 = s1; s1 = phd1; phd1 = s1;

Practice 2: do on the board 8.17 Look at the code below. You have four classes (O, X, T and M) and a variable of each of these. O o; X x; T t; M m; The following assignments are all legal: m = t; m = x; o = t; The following assignment are all illegal: o = m; o = x; x = o; What can you say about the relationships of these classes?

Inheritance Syntax public class Superclass { } public class Subclass extends Superclass

Inheritance code for Person public class Person { private String name; public Person() // constructor { // do constructor-like stuff } public Person(String n) { name = n; } public void getName( ) { return name; } // other methods } parent classes are no different from any classes we have written.

Code for Student public class Student extends Person inheritance is implemented with “extends” Code for Student public class Student extends Person { private String school; public Student( ) { super( ); school = "Christopher Newport University"; } public Student(String name, String school) { super(name); this.school = school; } public void getSchool( ) { return school; } } Must call parent constructor as first line of subclass constructor Must call parent constructor as first line of subclass constructor

Superclass constructor call Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) works only, if the superclass has a constructor without parameters Must be the first statement in the subclass constructor.

Client using Person and Student A. OK; B. NOT ok public class MyClient { Person p = new Person( ); Student s = new Student( ); Person r = new Student( ); public MyClient( ) p.getName( ); // 1 s.getSchool( ); // 2 s.getName( ); // 3 r.getSchool( ); // 4 }

Inheritance: what do we know? extends // used in subclass superclass has no indication inheritance is being used super() // calls constructor of superclass must be first statement of subclass