Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.

Slides:



Advertisements
Similar presentations
Class Hierarchy (Inheritance)
Advertisements

CS 211 Inheritance AAA.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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 Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Introduction to Inheritance
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
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.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
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.
COS 312 DAY 13 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Over due – Next progress report is March 26 Assignment 4 Posted – Chap 6 & 7 – Due.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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 Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
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.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Advanced Programming in Java
Chapter 8 Inheritance.
03/10/14 Inheritance-2.
Inheritance Chapter 5.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Overriding Methods & Class Hierarchies
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single vs. Multiple Inheritance Up-Casting

Unit 012 Recall What Inheritance is About Inheritance allows a software developer to derive a new class from an existing one. –Reusability The existing class is called the parent class, base class or super class. The new class is called the child class, derived class or subclass. As the name implies, the child inherits characteristics of the parent. That is, the child class inherits the methods and data defined in the parent class. Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class. Inheritance creates an is-a relationship, meaning the child is a more specific version of the parent.

Unit 013 The extends Keyword In Java, the keyword extends is used to establish an inheritance. 1.public class Student{ 2.private String name; 3.private int id; 4.private double gpa; 5. 6.public Student(int id, String name, double gpa){...} 7.public Student(int id, double gpa){...} 8.public String getName(){...} 9.public int getId(){...} 10.public double getGPA(){...} 11.public String toString(){...} 12.public void setGPA(double newGPA){...} 13.} 1.public class GraduateStudent extends Student{ 2.private String thesisTitle; 3.public GraduateStudent(...){...} 4.public String toString(){...} // overridden 5.public void defendThesis(){...} // added 6.}

Unit 014 The Object Class A class called Object is defined in the java.lang package of the Java standard class library. If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class. Thus, the Object class is the ultimate parent of all classes. The Object class contains a few useful methods, toString, equals, etc., which are inherited by all classes. The toString method in the Object class is defined to return the name of the object's class and its location in the memory, while the equals method determines if two references are aliases. Although all objects are guaranteed to have a toString and equals methods, it is often desirable to override these methods when we write our classes.

Unit 015 Overriding Methods A child class can override a method it inherited from its super class. The new method must have the same signature as the parent's method. 1.public class Student{ 2.private String name; 3.private int id; 4.private double gpa; 5.//... 6.public String toString(){ 7.return "ID: "+id+“ Name: "+name+“ GPA: "+gpa; 8.} 9.// } 1.public class GraduateStudent extends Student{ 2.private String thesisTitle; 3.//... 4.public String toString() { // overridden 5.return super.toString()+“ Thesis Title: "+thesisTitle; 6.} 7.//... 8.}

Unit 016 Overloading vs Overriding Comparison between overloading and overriding Overriding deals with two methods, one in a parent class and one in a child class with the same signature Overriding lets you define a similar operation in different ways for different object types Overloading deals with multiple methods in the same class with the same name but different signatures. Overloading lets you define a similar operation in different ways for different data

Unit 017 Constructors are not Inherited Constructors are not inherited, even though they have public visibility. However, the super reference can be used within the child's constructor to call the parent's constructor. In that case, the call to parent's constructor must be the first statement. 1.public class GraduateStudent extends Student{ 2.private String thesisTitle; 3.//... 4.public GraduateStudent(int id, String name, double gpa, String title){ 5.super(id, name, gpa); 6.thesisTitle = title; 7.} 8.//... 9.} If super is not used to call the parent's constructor, the compiler inserts a call to the parent's default constructor. An error results if no such default constructor exists.

Unit 018 What then is Inherited Fields and Methods are inherited if their access modifiers allow. public variables and methods are inherited, but private ones are not. When an instance of a subclass is created, so is that of its super class. GraduateStudent s = new GraduateStudent(202020, “Usman”, 3.7, “JavaMania”);

Unit 019 The protected Visibility Modifier We saw that fields that are declared private cannot be inherited. We also know that it is a very bad practice to declare fields as public. So is there a compromise? Yes, protected modifier. 1.public class Student{ 2.protected String name; 3.protected int id; 4.protected double gpa; 5.//... 6.} 1.public class GraduateStudent extends Student{ 2. private String thesisTitle; 3. // public String toString(){ 5. return "ID: "+id+"Name: "+name+"GPA: "+gpa+"ThesisTitle: "+thesisTitle; 6. } 7. //... 8.}

Unit 0110 Single vs. Multiple Inheritance Multiple inheritance allows a class to be derived directly from two or more classes, inheriting the members of all parents. Collisions, such as the same variable name in two parents, have to be resolved - leading to complex code. To avoid this complexity, Java supports only single inheritance, meaning that a derived class can have only one direct parent class. Student GraduateStudent defendThesis() Object

Unit 0111 Class Hierarchy A child class of one parent can be the parent of another child, forming class hierarchies. Two children of the same parent are called siblings - Example, Faculty and Staff. Good class design puts all common features as high in the hierarchy as is reasonable

Unit 0112 Up-Casting An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example, if the Employee class is used to derive a class, Faculty, then an Employee reference can be used to point to a Faculty object. This assigning is called up-casting, because we are going up the inheritance tree. Up-casting is considered to be a widening conversion since the subclass object has more methods than its superclass.

Unit 0113 Why Up-Casting The advantage of up-casting is that some methods apply to a superclass and all its subclasses. Such a method can be designed to accept a superclass reference so that it can be called with a superclass object or subclass object. For example, a method that generates pay-slip for all employees may have the following heading: Now since the method is expecting Employee as argument, we can call it with an object of MonthlyEmployee or HourlyEmployee. 1.MonthlyEmployee secretary = new MonthlyEmployee("Al-Amir", 8000); 2.HourlyEmployee consultant = new HourlyEmployee("Umar", 500); 3.//... 4.generatePaySlip(secretary); 5.generatePaySlip(consultant);

Unit 0114 Exercises Write each of the following classes: 1.A class, Person, with an instance variable, name. Write appropriate accessor and mutator methods for the class. Also write a toString method for the class. 2.Employee, that extends Person, and has: two additional instance variables, ID and payRate; and an additional method, pay, that returns the payRate. Override the toString method accordingly. 3.Faculty, that extends Employee, and has two additional variables; officeHours of type String and teachingCourses, which is an array of Courses. Each course has course code and course title. Write appropriate accessor, mutator and toString methods for the class. 4.TestPersons, that has two methods: void printPerson(Person p) that prints its argument. And a main method that creates an instance of each of the above classes and prints it using printPerson method.

Unit 0115 Example class A { String a = "A String"; String method1(){ return "method1() from class A."; } static String method2(){ return "method2() from class A."; } class ShadowingVsOverriding extends A { double a = 7; String method1(){ return "method1() from class ShadowingVsOverriding."; } static String method2(){ return "method2() from class ShadowingVsOverriding."; }

Unit 0116 Example – Cont’d public static void main(String args[]){ //A obj = new ShadowingVsOverriding(); ShadowingVsOverriding obj = new ShadowingVsOverriding(); System.out.println(obj.a); System.out.println(obj.method1()); System.out.println(obj.method2()); } 7.0 method1() from class ShadowingVsOverriding. method2() from class ShadowingVsOverriding. Press any key to continue...