CSC 205 Java Programming II

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Topics Inheritance introduction
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Chapter 13 - Inheritance.
Sections Inheritance and Abstract Classes
Inheritance In the real world, objects aren’t usually one-of-a-kind.
Inheritance and Polymorphism
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
Packages, Interfaces & Exception Handling
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 205 – Java Programming II
CSC 205 Programming II Lecture 2 Subclassing.
One class is an extension of another.
Figure 8.1 Inheritance: Relationships among timepieces.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Advanced Java Topics Chapter 9
CSC 205 – Java Programming II
Object Oriented Programming
Final and Abstract Classes
Unit-2 Objects and Classes
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

CSC 205 Java Programming II Inheritance

Topics What is Inheritance? Subclassing in Java When not to use inheritance? Overriding

Hierarchy Hierarchy is a ranking or ordering of abstractions Two most important hierarchies The “part-of” relationship, or aggregation The “is-a” relationship , or inheritance Inheritance, also known as subclassing Single inheritance: the only legal way in Java Multiple inheritance: supported in C++

Hierarchy: “is-a” relationship

Illustrating Inheritance Account specialization generalization SavingsAccount CheckingAccount

Subclassing in Java A subclass SavingsAccount: public class SavingsAccount extends Account { private double interestRate; public double getInterestRate() { return interestRate; } public void setInterestRate(double rate) { interestRate = rate;

Writing a Subclass A SavingsAccount object will contain two variables: balance, and interestRate Inherited from Account

Writing a Subclass Methods that can be applied to SavingsAccount objects: getInterestRate setInterestRate deposit (inherited) withdraw (inherited) getBalance (inherited) close (inherited)

When Not to Use Inheritance? Inheritance is one from many kinds of relationships among classes Don’t define an Account class by extending a Money class just because they are related Other relationships are available Aggregation: the “has-a” relationship Dependency: the “use” relationship

Hierarchy: “part-of” relationship

Aggregation & Composition University aggregation composition Student Department

The protected Access Modifier Four levels of accessibility for class members public protected (package or default) private The protected modifier allows for a subclass to access a member in its superclass

Overriding A subclass can replace some of its inherited behavior by overriding The toString() method Although a class method may not be overridden, it may be hidden