Inheritance & Method Overriding BCIS 3680 Enterprise Programming.

Slides:



Advertisements
Similar presentations
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Object Oriented Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Polymorphism, Abstract Classes & Interfaces
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Java – Inheritance.
Java Inheritance.
Inheritance and Polymorphism
Final and Abstract Classes
Presentation transcript:

Inheritance & Method Overriding BCIS 3680 Enterprise Programming

Overview  Inheritance  How to create a subclass  Methods in super- and subclasses  Constructors  Method overriding  Abstract methods  Interfaces 2

Inheritance  If we are dealing with a group of objects that share certain common traits while in the mean time each group sports some uniqueness of its own, then we may consider creating a class hierarchy, e.g.,  Different types of vehicles;  Different types of students.  A superclass is a general class that defines traits common to a set of related classes (subclasses).  A subclasses inherits those common variables and methods defined in the superclass. Each subclass has its own unique traits in addition to the shared ones. 3

A Sample Vehicle Hierarchy  This hierarchy is depicted using a Unified Modeling Language (UML) diagram.  In UML diagrams, hollow arrows point from the subclass to the superclass. 4

Superclasses and Subclasses  A superclass can have multiple subclasses.  A subclass, in turn, can be the superclass of other subclasses.  A subclass can inherit directly from only one superclass.  All classes in Java ultimately inherit from the Object class.  The object class contains a toString() method. Therefore, we can call the toString() method on all classes in Java, including those we define.  For an object, this method typically returns the class name and the object’s memory location. 5

Inheritance  The extends keyword class extends public class Graduate extends Student  A subclass inherits the variables and methods of a superclass. 6

Constructors  The constructor in the superclass initializes variables that are defined in the superclass.  Constructors are not inherited.  However, a subclass may have its own constructor(s).  Typically, a subclass constructor…  May call the constructor in the superclass to initialize inherited variables, i.e., those defined in the superclass; and then it has additional code to initialize those variables defined in the subclass.  May even initialized all variables (inherited or non-inherited) in its own way. 7

Accessing Superclass Members  To access a variable or method in the superclass, use the super keyword.  To access a variable – super.variableName  To access a method – super.methodName([args])  To access a constructor – super([args])  Notice when accessing a superclass constructor, no method name or class name is needed, e.g, if Graudate inherits from Student :  Correct: super(i, n, g, m, gpa);  Incorrect: super.Student(i, n, g, m, gpa); 8

Adding New Members 9  It would be pointless if a subclass didn’t need any unique variables and/or methods of its own.  Therefore, the subclass often adds its own variables.  It may add new methods and/or modify methods defined in the superclass.  How a subclass modify superclass-defined methods:  It may add to the behaviors defined in the superclass version.  It may even change the behaviors entirely.  This is called method overriding. In other words, the version in the subclass “overrides” the one defined in the superclass.

Method Overriding  When a method in a subclass has the same return type and signature as a method in its superclass (in other words, they have identical header), then the method in the subclass is said to override the method in the superclass.  Note that even if a method is not overidden, it still is accessible in the subclass by virtue of inheritance.  When called from an object of a subclass, (e.g., undergradObj.retrieveInfo() ):  If the method is overridden, the version of that method that is defined in the subclass is run.  If the method is not overriden, the version of that method that is defined in the superclass is run. 10

Rules for Overriding  The return type must be the same for both the overriding and overridden methods.  The parameter list must be exactly the same.  Static methods cannot be overridden. 11

Review: Overloading 12

Overloading vs. Overriding 13 OverloadingOverriding WhereWithin the same classBetween superclass and subclass PurposeTo have various versions of a method so that callers of the method have flexibility in using the method To change or add to an action (method) that is defined in the superclass so that the method defines how the subclass acts in its own way (as opposed to how the superclass or other subclass would act) Method NameMust be the same Parameter ListMust be differentMust be the same Return Data TypeNot relevantMust be the same

Overriding 14

Polymorphism 15  A reference variable can refer to and be used to store objects of types different from its own, as long as those types are subclasses of its type.  For example, Undergrad and Graduate are subclasses of Student. Therefore, all the following are legal:  Student s1 = new Student();  Student s2 = new Undergrad();  Student s3 = new Graudate();

Polymorphism 16  If you want to store 100 students sequentially in an array but won’t be able to tell until run time whether a student is undergraduate or graduate, then creating an array for undergraduate and another for graduate…  Functionally fails the business requirements (sequential storage while u/g status is random);  Coding wise is awkward.  You can declare the array as a Student array. Then in run time, regardless of whether a student is undergraduate or graduate, the object can be stored in the next available array element.

17

Polymorphism 18  With each element, you can call a method (defined in the superclass and possibly overriden in the subclasses) with the same statement, regardless of the type of the object (undergrad or graduate). for (int i = 0; i < sa.length; i++) { sa[i].retrieveInfo(); }

Abstract Methods 19  Sometimes, each subclass carries out a particular behavior in very different ways, but for some reasons (e.g., “polymorphism”), we want to define the behavior as a method at the superclass level, even though no specific code is possible due to the substantial variation among the subclasses.  In this case, we simply place a method header in the superclass, without method body.  Instead of the pair of curly braces and code contained within, we add only a semicolon to the end of the parameter list.  Such a method is called an abstract method.  Each subclass is responsible for fleshing out how the method works. In this case, the subclass implements (instead of “overrides”) the abstract method.

Defining an Abstract Method  To declare a method as abstract, include the abstract keyword in the method header, and end the header with a semicolon: accessModifier abstract returnType methodName( [parameter list] ); public abstract writeThesis();  The semicolon at the end of the header indicates that the method contains no code.  There are no opening and closing curly braces. 20

Abstract Classes 21  As long as there is one abstract method in a class, this class becomes an abstract class.  To make a class abstract, add the abstract keyword in the class header, accessModifier abstract class ClassName { // class body } public abstract class Student {... }  You cannot instantiate (create) an object of an abstract class. However, you can declare an object of that class.

Interface 22  If a subclass wants to be a concrete class (a class that can be instantiated), then it must implement all abstract methods in the abstract superclass.  This can be a bit rigid in scenarios where we want an abstract method or a set of abstract methods to be implemented:  By only some but not all subclasses in an inheritance hierarchy;  By classes from various inheritance hierarchies. Despite being from different hierarchies, these classes all display certain capabilities as suggested by the abstract methods.  The solution is to group these abstract methods into a special class called interface.  A interface name often contains the –able or –er / -or suffix.

Interface 23  If a class implements an interface, it fleshes out the behaviors by implementing abstract methods contained in the interface.  With interfaces, we can say, “This, this, and this class should be able to perform this set of actions, but we will let each of them decide how it actually performs those actions.”  For example, a dog, a cat, and a parrot are very different species, but they all can be a pet (being “petable”). However, how each is actually “petable” differs.  Similarly, a dog, a cat, and a baby all are “adorable”. But we will let each be “adorable” in his/her own ways.

24

25

Defining an Interface 26  An interface includes only abstract methods.  Syntax [public] interface InterfaceName { [public abstract] returnType1 methodName1 ([parameterList1]); [public abstract] returnType2 methodName2 ([parameterList2]); … }  The applicable access modifier is either public or none.

Implementing Interfaces 27  A class can implement one or more interfaces.  An interface can be implemented by any number of classes.  Syntax: class ClassName [extends SuperClassName] implements InterfaceName1, [interfaceName2, …]