Class Hierarchies, Inheritance and Interfaces CIS 304 Intermediate Java Programming for Business.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Inheritance University Of Ha’il.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
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.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
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 and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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.
Class Inheritance Part I
OOP: Encapsulation &Abstraction
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Lecture 12 Inheritance.
Objects as a programming concept
Java Programming Language
Interfaces.
Presentation transcript:

Class Hierarchies, Inheritance and Interfaces CIS 304 Intermediate Java Programming for Business

Motivations for OO Programming Coding that fosters reuse, multiple use, easier design, reliability, faster development, lower life-cycle cost brought about by … 4 Abstraction 4 Encapsulation 4 Inheritance

Motivations for OO Programming "Real World" doesn't "start from scratch" some objects resemble other objects some objects act the same (at least in some respects) as other objects 4 So, OO programming allows classes (from which objects are constructed) to inherit characteristics and behaviors from other classes

What Do These Classes (objects) Have in Common? Bank Acct Savings Acct Checking Acct Now, in what ways do these differ?

Need to Differentiate "is a" Relationship from a "has a" Relationship! Bank Acct Savings Acct Checking Acct Is a Bank Acct a Savings Acct? Is a Savings Acct a Bank Acct? Does a Bank Acct have a customer name? an origination date?

Creating a Hierarchical Relationship between Classes in Java public class BankAcct { } public class SavingsAcct extends BankAcct { }

What is Shared Between Superclass and Subclass -- instance variables public class BankAcct { String acctID; Date origDate; } public class SavingsAcct extends BankAcct { //each SavingsAcct object will have an acctID and // origDate instance variable }

What is Shared Between Superclass and Subclass -- methods public class BankAcct { public void setAcctID(String acctID) {this.acctID = acctID;} public int closeAcct(String acctID) {… code here …} } public class SavingsAcct extends BankAcct { //each SavingsAcct object will be able to use the methods //setAcctID and closeAcct }

Keyword super 4 We know that the keyword this refers within the code of an object to the current object itself 4 Similarly, the keyword super refers within the code of an object to the superclass of the current object

Term: overloading 4 Recall that the signature of a method uniquely identifies a method 4 The signature consists of the name of the method and the number, type, and order of the arguments required of the method 4 Example: if you define the method public void trialMethod(int myInt, String myString) {… code here …} and then call it with trialMethod("Georgia Jones") then Java will say that you have not defined the method trialMethod(String)

Term: overloading 4 Method overloading, usually just referred to as overloading, is when you have more than one method that uses the same method name 4 So methods public void sampleMethod() {…code here...} public void sampleMethod(double pay) {...} public void sampleMethod(int myInt, String a) {...} are overloaded 4 How does Java know which method to use?

Term: overriding 4 When methods in a subclass and its superclass have the same signature, this is called method overriding 4 Before, the signature told Java which method to use, but here we have the chance of two methods with the same signature. So, what does Java do? 4 Where there would otherwise be ambiguity, Java will use the method of the subclass (i.e., the more specific prevails) 4 If you want to access the superclass' method with the same signature you can by using the keyword super. for example super.methodWithSameName()

Overriding Bank Acct Savings Acct Checking Acct If you are in a SavingsAcct object and use findBal(), Java needs to know -- and does decide -- which findBal() method you are referring to? Each of these classes can have its own method named findBal()

protected visibility Bank Acct Savings Acct Checking Acct If you make the instance variables of BankAcct protected and not private, then all the subclasses of BankAcct can access the instance variables directly If the instance variables in BankAcct are private, they can't be seen or changed by code in SavingsAcct or CheckingAcct

Class Object Bank Acct Savings Acct Checking Acct Object Recall: this is the real hierarchy, with object at the top level

Assignment Declarations: Object myObj, theObj; BankAcct aBankAcct; SavingsAcct mySavings; Which assignments statements are OK? myObj = aBankAcct; aBankAcct = mySavings; theObj = mySavings; mySavings = aBankAcct; aBankAcct = theObj;

Casting 4 You can not make the assignment mySavings = aBankAcct; but if you know that the object aBankAcct is actually a SavingsAcct object, you can cast it down the hierarchy using mySavings = (SavingsAcct) aBankAcct; 4 Another example (if theObj is actually a BankAcct object) aBankAcct = (BankAcct) theObj;

Casting -- Problems that Occur 4 If you do the cast mySavings = (SavingsAcct) aBankAcct; and aBankAcct is not actually a SavingsAcct object, a Class Cast Exception is thrown this will bomb your program if the Exception is not caught