Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
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
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
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 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
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.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance using Java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“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
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
EE2E1. JAVA Programming Lecture 3 Inheritance. Contents Base classes and derived classes Base classes and derived classes Protected scope Protected scope.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
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.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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 in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
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.
Advanced Programming in Java
Object-Orientated Analysis, Design and Programming
Lecture 12 Inheritance.
Overriding Method.
03/10/14 Inheritance-2.
ATS Application Programming: Java Programming
Chapter 9 Inheritance and Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
F II 8. More on Inheritance Objectives
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa

Inheritance Inheritance is the mechanism by which attributes and behaviours are passed by a ‘superclass’ to a ‘subclass’ the subclass does not need to re- implement existing behaviour, it can use the existing behaviour and data by inheriting it.

Why inherit Person <- Student <- MScStudent <- ResearchStudent <- Customer <- CorporateCustomer <- individualCustomer

What to inherit Subclass inherits from a Superclass all (non- private) superclass behaviours and attributes The subclass even receive all the relationships of its superclass. The subclass may provide additional attributes and behaviours, relevant to its specialised level of abstraction. subclass may modify the behaviours it inherits from its superclass.

Terminology Given a subclass MySub and a superclass MySuper. Then we say… –MySub inherits from MySuper. –MySub is a subclass of MySuper. –MySub is a specialisation of MySuper. –MySuper is a superclass of MySub. –MySuper is a generalisation of MySub.

When to use inheritance Avoid duplication in functionality. There is some common functionality. But remember! Don’t confuse object instances with inheritance. Inheritance implies different types, not different instances of the same type. –Examples in class

Student activity 8.1 Give an example of Inheritance –Think of something that we have not mentioned so far.

Implementing inheritance in Java Java allows you to derive new classes from existing classes using “extends” Example: student class and MSc student class. Student class: –Attributes: name, mark –Methods: getMark, setMark. MScStudent class could be derived from the Student class, therefore inheriting the variables and methods contained in the Student class. –New attribute: supervisor and method: chooseSupervisor

An example of inheritance: Student public class Student { private String name; private int mark; public void setName(String n){ name=n;} public String getName() { return name;} public void setMark(int m) { mark=m;} public int getMark(){ return mark;} }

An example of inheritance: MScStudent public class MScStudent extends Student { private String supervisor; public void chooseSupervisor(String nm) { supervisor=nm;} public String getSupervisor() { return supervisor;} }

Using MScStudent public class TestInheritance { public static void main(String [] args) { MScStudent s2=new MScStudent(); s2.setName("Mike"); s2.chooseSupervisor("Jane"); System.out.println("s2 is "+ s2.getName()+ "--Supervisor is "+s2.getSupervisor()); }

Student activity 8.2 Implement Student and MScStudent Explore private, public attributes –Add a new method in the MScStudent class. The new method should change the value of the variable name. e.g. public String getName(){return name;} –Can you compile MScStudent now? Explain why and post your explanation to the module forum.

The protected modifier and visibility modifiers revisited A protected data or a protected method can be accessed or invoked by any class in the same package or its subclasses, even if the subclasses are in a different package.

Using the key word super and calling superclass constructors public class Student { private String name; Student(String nm){ name=nm;} … } public class MScStudent extends Student { private String supervisor; MScStudent(String nm){ super(nm);} … }

Overriding methods 1 public class Student { private String name; int mark; … other methods public String toString() { return "Name: "+name+ "Mark: "+mark; }

Overriding methods 2 public class MScStudent extends Student { private String supervisor; MScStudent(String nm){ super(nm);} … other methods public String toString(){ String ss=super.toString(); return ss+“ supervisor: "+supervisor; } }

Polymorphism via inheritance, and casting public class TestPoly { public static void main(String [] args) { Student s1=new Student("Kate"); System.out.println(s1.toString()); s1=new MScStudent("Mike"); System.out.println(s1.toString()); }

Polymorphic The term polymorphism: “having many forms”. A polymorphic reference is a reference variable that can refer to different types of objects at different points in time An example of polymorphism is the technique by which a reference that is used to invoke a method can actually invoke different methods at different times depending on what it is referring to at the time. Polymorphism provides a means to an elegant versatility in our software. It allows us to provide a consistent approach to different but related behaviours.

Example public class TestPoly2 { public static void main(String [] args) { Student [] all= new Student[3]; all[0]= new Student("kate"); all[1]= new MScStudent("mike"); all[2]= new Student("Jane"); for (int i=0;i<3;i++) System.out.println(all[i].toString()); }

Casting 1 Casting can be used to convert an object of one class type to another within an inheritance hierarchy. There are implicit casting and explicit casting.

Casting 2 Student s=new MScStudent(“Mike”); assigns an MScStudent object to a reference of the Student type. This statement involves an implicit casting. This is legal because an instance of MScStudent is an instance of Student.

Casting 3 Explicit casting must be used when casting an object from a superclass to a subclass. Student s=new MScStudent("Mike"); s.chooseSupervisor(“Helen”); s is a reference of type Student. Since the Student class does not have the method chooseSupervisor, the compiler doesn’t know that the object pointed by s has the chooseSupervisor method. So, we have to explicitly cast s to MScStudent as follows: ((MScStudent)s).chooseSupervisor(“Helen”);

Must have toString in the superclass Student [] all= new Student[3]; all[0]= new Student("kate"); all[1]= new MScStudent("mike"); all[2]= new Student("Jane"); for (int i=0;i<3;i++) System.out.println(all[i].toString());

Summary In this section, we have explained how to derive subclasses from super classes through inheritance the use of the visibility modifiers the use of the key word super how to override methods what is casting the concept of polymorphism and the applications