Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
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.
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.
Inheritance Inheritance Reserved word protected Reserved word super
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.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10: Inheritance and Polymorphism
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Polymorphism &Virtual Functions
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1. Everything is an object 2. A program is a set of objects that interact by sending messages (i.e. function calls) 3. Each object has its own state 4.
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.
Java Implementation: Part 3 Software Construction Lecture 8.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Object-Oriented Programming: Polymorphism Chapter 10.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Object Oriented Programming
Extending Classes.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Java – Inheritance.
Java Inheritance.
C++ Polymorphism Reference and pointer implicit type casting
Inheritance Lakshmish Ramaswamy.
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Inheritance Lakshmish Ramaswamy

Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles Calculating the total area public static double totalArea (WhatType [ ] arr){ double total = 0.0; for (int i = 0; i < arr.length; i ++){ if(arr[i] != null) total += arr[i].area()} return total; }

Example contd. Need type declaration of WhatType –Say “Shape” Inherit Circle and Rectangle from Shape Shape needs to have area method But, how to implement area method in Shape? Shape Circle Rectangle Square

A Possible Implementation

Pros and Cons Advantages –New classes can be added to hierarchy without changing existing implementations –No instanceof tests –No casting necessary Disadvantage –What if someone creates a Shape object and assigns to an array location? arr [5] = new Shape(); –Wrong results from totalArea unless additional tests are performed

What is Really Happening? Area method is a placeholder –Not to be called directly –Programmer is trying to signal an error –Just there so that the compiler does not complain In fact the Shape class itself is a placeholder –Common superclass for other classes –An abstract concept

Abstract Methods & Classes A method that just declares the functionality Does not provide a default implementation All derived class objects must eventually implement –Each object needs to provide its own implementation Shape class “declares” the area method –Must be implemented in Circle and Rectangle

Abstract Class Class having at least one abstract method Cannot construct an abstract class –Compiler will check for violations Might declare one or more constructors Might also provide implementations of constructors –Will be used when derived classes use super Can have non-abstract methods

Abstract Class (contd.) Can declare both static and non-static fields All abstract classes must be explicitly declared in Java A derived class that does not implement an abstract method remains abstract –Hence, they must be declared “abstract”

Shape as abstract class Shape a, b, c, d; a = new Circle (3.0); b = new Square (4.0); c = new Rectangle (3.5, 4.5); d = new Shape (); // illegal

Methods Summary Final methods – Cannot be overridden in derived classes –VM may perform inline optimization at runtime Abstract method – Overriding resolved at runtime using dynamic dispatch Static method – Resolved at compile time as there is no controlling object Normal methods – Resolved at runtime using dynamic dispatch

Multiple Inheritance A class being derived from more than one base class –Ex: A StudentEmployee Class being derived both from Student and Employee Some OO languages (ex: C++) support multiple inheritance Java does not allow multiple inheritance

Issues with Multiple Inheritance Does StudentEmployee get two copies of name, address and phone number? If StudentEmployee does not override toString method, which implementation would be used? If a method is declared to be final in one base class and not in the other, would the StudentEmployee be allowed to override the method?

Interface Alternative to multiple inheritance The ultimate abstract class Consists only of public abstract methods & public static final fields –No implementation of any method Class providing definitions of all abstract methods is said to implement the interface Multiple interfaces do not suffer from same problems as multiple inheritance Class can implement multiple interfaces but extend from a single class

Specifying Interface Similar to a class declaration but uses the keyword interface Lists methods that need to be implemented Class implementing an interface should do two things –Declare that it implements the interface using “implements” – Provide implementations of all methods declared in interface A method implementing an interface may remain abstract

Example of Interface

Example of Implementing Class

Multiple Interfaces Java allows multiple interfaces List all the interfaces a class is implementing Provide implementations of all methods in all interfaces public class X implements a, b { … }