1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Class Hierarchy (Inheritance)
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.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
CS 211 Inheritance AAA.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Introduction To Scientific Programming Chapter 7 – Inheritance or (More on Classes)
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
ITM 352 Class inheritance, hierarchies Lecture #.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
HST 952 Computing for Biomedical Scientists Lecture 6.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
COMP Inheritance Basics Yi Hong June 09, 2015.
Coming up: Inheritance
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Classes (Part 1) Lecture 3
Inheritance and Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces
Inheritance Chapter 7 Chapter 7.
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Java – Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 7 Inheritance.
Presentation transcript:

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance

2 Principles of OOP l OOP - Object-Oriented Programming l Principles of OOP: »Information Hiding »Encapsulation »Polymorphism »Inheritance

3 Why OOP? l To try to deal with the complexity of programs l To apply principles of abstraction to simplify the tasks of writing, testing, maintaining and understanding complex programs l To increase code reuse »to reuse classes developed for one application in other applications instead of writing new programs from scratch ("Why reinvent the wheel?") l Inheritance is a major technique for realizing these objectives

4 Inheritance Overview l Inheritance allows you to define a very general class then later define more specialized classes by adding new detail »the general class is called the base or parent class l The specialized classes inherit all the properties of the general class »specialized classes are derived from the base class »they are called derived or child classes l After the general class is developed you only have to write the "difference" or "specialization" code for each derived class l A class hierarchy: classes can be derived from derived classes (child classes can be parent classes) »any class higher in the hierarchy is an ancestor class »any class lower in the hierarchy is a descendent class

5 An Example of Inheritance: a Person Class The base class: ( next slide) Constructors: »a default constructor »one that initializes the name attribute (instance variable) l Accessor methods: »setName to change the value of the name attribute »getName to read the value of the name attribute »writeOutput to display the value of the name attribute l One other class method: »sameName to compare the values of the name attributes for objects of the class Note: the methods are public and the name attribute private

6

7 Derived Classes: a Class Hierarchy l The base class can be used to implement specialized classes »For example: student, employee, faculty, and staff l Classes can be derived from the classes derived from the base class, etc., resulting in a class hierarchy Person StudentEmployee Faculty Staff UndergraduateGraduate MastersDegree NonDegree PhD

8 Constructor Chaining Constructors cannot be inherited Constructors have one purpose in life: to create an instance of a class l The first line of every constructor must be either A this call to another constructor in the same class.  Constructors use this to refer to another constructor in the same class with a different parameter list. l A super call to a parent constructor. l If no constructor call is written as the first line of a constructor, the compiler automatically inserts a call to the parameterless superclass constructor

9 class ConstructorChain { public static void main(String[] args) { Child c = new Child(); } Constructor Chaining

10 class Child extends Parent { Child() { System.out.println("Child() constructor"); } Constructor Chaining

11 class Parent extends Grandparent { Parent() { this(25); System.out.println("Parent() constructor"); } Parent(int x) { System.out.println("Parent(" + x + ") constructor"); } Constructor Chaining

12 class Grandparent { Grandparent() { System.out.println("Grandparent() constructor"); } Constructor Chaining

13 Example of Adding Constructor in a Derived Class: Student l Two new constructors (one on next slide) »default initializes attribute studentNumber to 0 super must be first action in a constructor definition »Included automatically by Java if it is not there »super() calls the parent default constructor public class Student extends Person { private int studentNumber; public Student() { super(); studentNumber = 0; } … Keyword extends in first line »creates derived class from base class »this is inheritance The first few lines of Student class

14 Example of Adding Constructor in a Derived Class: Student  Passes parameter newName to constructor of parent class  Uses second parameter to initialize instance variable that is not in parent class. public class Student extends Person {... public Student(String newName, int newStudentNumber) { super(newName); studentNumber = newStudentNumber; }... More lines of Student class

15 Example of Adding an Attribute in a Derived Class: Student l Note that an attribute for the student number has been added »Student has this attribute in addition to name, which is inherited from Person A line from the Student class: private int studentNumber;

16 Example of Overriding a Method in a Derived Class: Student Both parent and derived classes have a writeOutput method l Both methods have the same parameters (none) »they have the same signature l The method from the derived class overrides (replaces) the parent's l It will not override the parent if the parameters are different (since they would have different signatures) l This is overriding, not overloading public void writeOutput() { System.out.println("Name: " + getName()); System.out.println("Student Number : " studentNumber); }

17 Call to an Overridden Method Use super to call a method in the parent class that was overridden (redefined) in the derived class Example: Student redefined the method writeOutput of its parent class, Person Could use super.writeOutput() to invoke the overridden (parent) method public void writeOutput() { super.writeOutput(); System.out.println("Student Number : " studentNumber); }

18 public class SpinningActor extends Actor { public void act() { setDirection(getDirection()+Location.HALF_RIGHT); } public void act() { setDirection(getDirection() + Location.HALF_CIRCLE); } Overriding

19 Overriding Verses Overloading Overriding l Same method name l Same signature l One method in ancestor, one in descendant Overloading l Same method name l Different signature l Both methods can be in same class

20 The final Modifier l Specifies that a method definition cannot be overridden with a new definition in a derived class l Example: public final void specialMethod() {... l Used in specification of some methods in standard libraries l Allows the compiler to generate more efficient code l Can also declare an entire class to be final, which means it cannot be used as a base class to derive another class

21 private & public Instance Variables and Methods private instance variables from the parent class are not available by name in derived classes »"Information Hiding" says they should not be »use accessor methods to change them, e.g. reset for a Student object to change the name attribute private methods are not inherited! »use public to allow methods to be inherited »only helper methods should be declared private

22 What is the "Type" of a Derived class? l Derived classes have more than one type l Of course they have the type of the derived class (the class they define) l They also have the type of every ancestor class »all the way to the top of the class hierarchy All classes derive from the original, predefined class Object Object is called the Eve class since it is the original class for all other classes

23 Assignment Compatibility l Can assign an object of a derived class to a variable of any ancestor type Person josephine; Employee boss = new Employee(); josephine = boss; l Can not assign an object of an ancestor class to a variable of a derived class type Person josephine = new Person(); Employee boss; boss = josephine; Not allowed OK Person Employee Person is the parent class of Employee in this example.

24 How do Programs Know Where to Go Next? l Programs normally execute in sequence l Non-sequential execution occurs with: »selection (if/if-else/switch) and repetition (while/do- while/for) (depending on the test it may not go in sequence) »method calls, which jump to the location in memory that contains the method's instructions and returns to the calling program when the method is finished executing l One job of the compiler is to try to figure out the memory addresses for these jumps l The compiler cannot always know the address »sometimes it needs to be determined at run time

25 Static and Dynamic Binding l Binding: determining the memory addresses for jumps l Static: done at compile time »also called offline l Dynamic: done at run time l Compilation is done offline »it is a separate operation done before running a program l Binding done at compile time is, therefor, static, and l Binding done at run time is dynamic »also called late binding

26 Example of Dynamic Binding: General Description l Derived classes call a method in their parent class which calls a method that is overridden (defined) in each of the derived classes »the parent class is compiled separately and before the derived classes are even written »the compiler cannot possibly know which address to use »therefore the address must be determined (bound) at run time

27 Polymorphism l Using the process of dynamic binding to allow different objects to use different method actions for the same method name l Originally overloading was considered to be polymorphism l Now the term usually refers to use of dynamic binding

28 Summary l A derived class inherits the instance variables & methods of the base class l A derived class can create additional instance variables and methods l The first thing a constructor in a derived class normally does is call a constructor in the base class l If a derived class redefines a method defined in the base class, the version in the derived class overrides that in the base class l Private instance variables and methods of a base class cannot be accessed directly in the derived class l If A is a derived class of class B, than A is both a member of both classes, A and B »the type of A is both A and B