Inheritance Dwight Deugo Nesa Matic

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Inheritance using Java
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
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.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
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.
Workbench Overview Dwight Deugo Nesa Matic
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
JUnit Dwight Deugo Nesa Matic
Programming in Java CSCI-2220 Object Oriented Programming.
JUnit Dwight Deugo Nesa Matic
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Classes Dwight Deugo Nesa Matic
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object-Oriented Principals Dwight Deugo Nesa Matic
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
Collections Dwight Deugo Nesa Matic
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Java IDE Dwight Deugo Nesa Matic
 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 Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Web Design & Development Lecture 9
Inheritance and Polymorphism
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Java – Inheritance.
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Presentation transcript:

Inheritance Dwight Deugo Nesa Matic

2 © , Espirity Inc. Additional Contributors None as of September, 2004

3 © , Espirity Inc. Module Overview 1.Inheritance 2.Interfaces 3.Type Hierarchy in Eclipse

4 © , Espirity Inc. Module Road Map 1.Inheritance  What is inheritance?  Class hierarchies  Method lookup in Java  Use of this and super  Constructors and inheritance  Abstract classes and methods 2.Interfaces 3.Type Hierarchy in Eclipse

5 © , Espirity Inc. What is Inheritance? Inheritance is one of the main object- oriented principals  It promotes reuse  It is a mechanism for sharing commonalities between classes Commonalities are stored in a parent class  Also called superclass Commonalities are shared between children classes  Also called subclasses

6 © , Espirity Inc. Specialization and Generalization Policy client premium policyNumber getClient setClient … HomePolicy house getHouse setHouse AutoPolicy auto getAuto setAuto LifePolicy generalization specialization

7 © , Espirity Inc. Defining Inheritance In Java, inheritance is supported by using keyword extends  It is said that subclass extends superclass  If class definition does not specify explicit superclass, its superclass is Object class public class Policy{… public class HomePolicy extends Policy{… public class AutoPolicy extends Policy{… public class LifePolicy extends Policy{… public class Policy{…public class Policy extends Object{…

8 © , Espirity Inc. Variables and Inheritance Variables can be declared as generic types, and assigned objects of more specific types  Variable declared as of type Policy can be assigned an instance of any Policy’s subclasses Policy policy; Policy = new Policy(); Policy policy; Policy = new HomePolicy(); Policy policy; Policy = new AutoPolicy(); Policy policy; Policy = new LifePolicy();

9 © , Espirity Inc. Multiple Inheritance Not supported in Java  A class cannot extend more than one class  There is only one direct superclass for any class  Object class is exception as it does not have superclass

10 © , Espirity Inc. What is Inherited? In general all subclasses inherit from superclass:  Data  Behavior When we map these to Java it means that subclasses inherit:  Fields (instance variables)  Methods

11 © , Espirity Inc. Inheriting Fields All fields from superclasses are inherited by a subclass  Inheritance goes all the way up the hierarchy Policy HomePolicy client premium policyNumber client premium policyNumber house client premium policyNumber house

12 © , Espirity Inc. Inheriting Methods All methods from superclasses are inherited by a subclass  Inheritance goes all the way up the hierarchy getClient setClient getPremium setPremium getPolicyNumber setPolicyNumber getHouse setHouse getClient setClient getPremium setPremium getPolicyNumber setPolicyNumber getClient setClient getPremium setPremium getPolicyNumber setPolicyNumber getHouse setHouse Policy HomePolicy

13 © , Espirity Inc. Method lookup begins in the class of that object that receives a message If method is not found lookup continues in the superclass HomePolicy getHouse setHouse house Method Lookup … HomePolicy homePolicy = new HomePolicy(); … homePolicy.getPremium(); HomePolicy class – method not found Policy getPremium setPremium premium Policy class – method found

14 © , Espirity Inc. this vs. super They are both names of the receiver object The difference is where the method lookup begins:  this  Lookup begins in the receiver object’s class  super  Lookup begins in the superclass of the class where the method is defined

15 © , Espirity Inc. Example public void print(){ System.out.println("A " + getClass().getName() + ", $" + getPremium()); } public void print(){ super.print(); System.out.println("for house " + getHouse().toString(); } policy.print(); homePolicy.print(); Policy HomePolicy A Policy, $1, Console A HomePolicy, $1, for house 200 Great Street Console

16 © , Espirity Inc. Method Overriding If a class define same method as its superclass, it is said that the method is overridden  Method signatures must match Policy HomePolicy //Method in the Policy class public void print(){ System.out.println("A " + getClass().getName() + ", $" + getPremium()); } //Overridden method in the HomePolicy class public String toString(){ super.print(); System.out.println("for house " + getHouse().toString(); }

17 © , Espirity Inc. Overriding Constructors Similar to overriding methods, applying rules specific to constructors:  First line in the constructor must be either this(parameters) or super(parameters)  This eventually leads to the Object class constructor that creates the object  If the call is not coded explicitly then an implicit zero- argument super() is called  If the superclass does not have a zero-argument constructor, this causes an error

18 © , Espirity Inc. Example of Overriding Constructors public Policy(double premium, Client aClient, String policyNumber){ this.premium = premium; this.policyNumber = policyNumber; this.client = aClient; } public HomePolicy(double premium,Client aClient,String policyNumber,House aHouse){ super(premium, aClient, policyNumber); this.house = aHouse; } Policy HomePolicy

19 © , Espirity Inc. Abstracts Classes Classes that cannot have instances  They are designed to hold inherited fields and methods for subclasses They also define what subclasses should implement  Details are left for concrete implementation in subclasses Usually specified at the design level

20 © , Espirity Inc. Defining Abstract Classes Modifier abstract is used to indicate abstract class public abstract class Policy {… Policy HomePolicy AutoPolicyLifePolicy

21 © , Espirity Inc. Abstract Methods Can only be defined in abstract classes  Abstract classes can contain concrete methods as well  Declaration of abstract method in concrete class will result in compile error Declare method signatures  Implementation is left to the subclasess  Each subclass must have concrete implementation of the abstract method Used to impose method implementation on subclasses

22 © , Espirity Inc. Defining Abstract Methods… Modifier abstract is also used to indicate abstract method public abstract class Policy{ public abstract void calculateFullPremium(); Policy calculateFullPremium HomePolicy AutoPolicyLifePolicy

23 © , Espirity Inc. …Defining Abstract Methods All subclasses must implement all abstract methods public class HomePolicy{ public void calculateFullPremium(){ //calculation may depend on a criteria about the house } public class AutoPolicy{ public void calculateFullPremium(){ //calculation may depend on a criteria about the auto } public class LifePolicy{ public void calculateFullPremium(){ //calculation may depend on a criteria about the client }

24 © , Espirity Inc. Module Road Map 1.Inheritance 2.Interfaces  What is an interface?  Defining interfaces  Using Interfaces 3.Type Hierarchy in Eclipse

25 © , Espirity Inc. What is Interface? Interfaces define a type and protocol but do not provide implementation of that protocol  Classes that implement interfaces must provide implementation for defined protocol  Interfaces capture common behavior of the classes that implement them Commonly used to:  Impose protocol on set of classes that implement interface  Indicate certain Java type (marker interfaces)

26 © , Espirity Inc. Interfaces Define Types Interfaces are, in a way, a higher abstraction of Java types  They define common protocol  Are commonly used where types need to be imposed but implementation is not important  For example in distributed programming Interfaces are used to impose typing  If variable is declared as of interface type, than instance of any class that implements that interface can be assigned to that variable

27 © , Espirity Inc. Defining Interface Similar to defining classes  Keyword interface used instead of class keyword  Defined methods contain signatures only Interfaces are also stored in.java files public interface Policyable{ public Policy createPolicy(); } public interface Transferable{ }

28 © , Espirity Inc. Rules Interfaces can contain:  Only method signatures  Only final static fields Interfaces cannot contain:  Any fields other than final static fields  Any static methods  Any method implementation  Any constructors

29 © , Espirity Inc. Classes and Interfaces At the design level interfaces are usually indicated with a stereotype > Instead of solid line, there is a dashed line between a class and interfaces it implements > Policyable Building House

30 © , Espirity Inc. Implementing Interfaces Classes implement interfaces  Keyword implements is used  They must define all methods that interface they implement declares public class House extends Building implements Policyable{ public Policy createPolicy(){ HomePolicy policy = new HomePolicy(this); return policy; }

31 © , Espirity Inc. Interfaces are Inherited It is possible that one interface extends other interfaces  Known as sybtyping in Java  Multiple inheritance is allowed with interfaces Inheritance works the same as with classes  All methods defined are inherited  There is a hierarchy of interfaces that exists in parallel to class hierarchy

32 © , Espirity Inc. Extending Interfaces Car Color ColoredCar public interface ColoredCar extends Car, Color { public String getSpeed(); public String getBaseColor(); } public interface Car { public String getSpeed(); } public interface Color { public String getBaseColor(); }

33 © , Espirity Inc. Naming Conventions There are few conventions when naming interfaces:  Suffix able is used for interfaces  Cloneable, Serializable, and Transferable  Nouns are used for implementing classes names, and I + noun for interfaces  Interfaces: IColor, ICar, and IColoredCar  Classes: Color, Car, and ColoredCar  Nouns are used for interfaces names, and noun+Impl for implementing classes  Interfaces: Color, Car, and ColoredCar  Classes: ColorImpl, CarImpl, and ColoredCarImpl

34 © , Espirity Inc. Cross-Hierarchy Polymorphism Allows classes across different hierarchies to share common protocols  Supported and imposed by use of interfaces > Policyable Building House Vehicle Auto

35 © , Espirity Inc. Implementation public class PolicyFactory{ public Policy createPolicy(House aHouse){ return aHouse.createPolicy(); } public Policy createPolicy(Auto anAuto){ return aHouse.createPolicy(); } … } public class PolicyFactory{ public Policy createPolicy(Policyable aPolicyable){ return aPolicyable.createPolicy(); }

36 © , Espirity Inc. Module Road Map 1.Inheritance 2.Interfaces 3.Type Hierarchy in Eclipse  Java types  Type casing  Eclipse type hierarchy  Supertype and subtype hierarchies

37 © , Espirity Inc. Java Types Variable can be declared as:  Reference type  Any instance of that class or any of the subclasses can be assigned to the variable  Interface type  Any instance of any class that implements that interface can be assigned to the variable Policy policy; policy = new AutoPolicy(); policy = new HomePolicy(); Policable policableObject; policableObject = new Auto(); policableObject = new House();

38 © , Espirity Inc. Variables and Messages If variable is defined as a certain type, only messages defined for that type can be sent to the variable  This may cause a problem if variable is assigned to an instance of a subtype that defines different messages Policy AutoPolicy getAuto() Policy policy; policy = new AutoPolicy(); policy.getAuto(); //ERROR

39 © , Espirity Inc. Type Casting Allows for sending messages not defined by the declared superclass type by casting variable to the actual subclass type This type cast is temporary  The declared type of the variable stays the same  Allows for messages to be sent to subtype Policy policy; policy = new AutoPolicy(); ((AutoPolicy)policy).getAuto();

40 © , Espirity Inc. Legal Type Casting? Typecasting can be done on any subtypes of the declared type A B CD //let’s say that message() is defined //in class A //declare variable of type A A variable; //assign instance of class E to the //variable variable = new E(); ((B)variable).message(); //VALID ((A)variable).message(); //VALID ((D)variable).message(); //VALID ((E)variable).message(); //VALID ((C)variable).message(); //NOT VALID E

41 © , Espirity Inc. Eclipse and Inheritance When creating class in Eclipse, wizards can be used to setup class details related to the inheritance  Abstract classes and super class specified Eclipse has built-in support for viewing and browsing different type hierarchies  These include supertype and subtype hierarchies

42 © , Espirity Inc. Eclipse Type Hierarchy Opened by choosing Open Type Hierarchy option from the context menu on the class Hierarchy View that opens represents a hierarchy of the chosen class

43 © , Espirity Inc. Supertype Hierarchy Opened by click on the Supertype Hierarchy toolbar button of the Hierarchy view  Hierarchy View shows the class and its superclasses

44 © , Espirity Inc. Subtype Hierarchy Opened by click on the Subtype Hierarchy toolbar button of the Hierarchy view  Hierarchy View shows the class and its subclasses

45 © , Espirity Inc. Review In this module we discussed:  Inheritance  Method lookup in Java  Use of this and super  Abstract classes and methods  Interfaces  Classes and interfaces  Polymorphism and inheritance  Type hierarchies in Eclipse

46 © , Espirity Inc. Labs! Lab: Inheritance