Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance Inheritance Reserved word protected Reserved word super
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.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter 3  Implementing Classes 1 Chapter 3 Implementing Classes.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
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.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
 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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
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.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
 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.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Chapter 12 – Object-Oriented Design
Lecture 3 John Woodward.
Chapter 3 – Implementing Classes
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Implementing Classes Yonglei Tao.
Computing with C# and the .NET Framework
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
CSC 205 Java Programming II
Chapter Three - Implementing Classes
Chapter 3 Implementing Classes
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Agenda About Homework for BPJ lesson 36 About practice of last class
Adapted from Java Concepts Companion Slides
AN INTRODUCTION TO OBJECTS AND CLASSES
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class

RelationshipSymbol Line Style Arrow Tip Inheritance-is-a SolidTriangle Interface Implementation DottedTriangle Aggregation-has-a Solid Diamond Dependency DottedOpen UML Relationship Symbols  Aggregation is a stronger form of dependency-see page 467

Place multiplicity notations near the ends of an association/ dependency. Multiplicity (Cardinality)

GJY 1..*

 Establishes has-a relationship/association between classes  BankAccount  One can navigate from one class to another  instantiation in Java – (note NOT inheritance)

package bankaccount; public class BankAccount { private double balance; public BankAccount () { balance=0; } public void deposit (double amount) { balance=balance+amount; } public void withdraw (double amount){ balance=balance - amount; } public double getBalance () { return balance; } package bankaccount; public class Bank { public static void main(String[] args) { BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit( ); harrysChecking.withdraw(500.00); System.out.println(harrysChecking.getBalance()); } Review BankAccount

Lab: Part 1 1.Add the interest method to your BankAccount Class 2. Add an account with your name 3. Call interest for Harry and pass 5% or Call the interest method for your account and pass the parameter as Revise VISIO drawing accordingly

Chapter 8, pp  pre & post conditions  Section 8.5

 Documentation included in form of comments ◦ Prior to a class ◦ Prior to a method ◦ Begins with “/**” and ends with “*/”  Internal Method comments discouraged

Objective: Tells other programmers how to use the method! precondition documentation postcondition documentation

Precondition: describes the state of relevant identifiers prior to the function's execution. Includes a reference to what variables are passed to the function. Postcondition: describes the state after the function is executed: the state of attributes after execution the return value if any.

Do NOT: 1) Repeat code description! (code is hidden and user/programmer on needs to know what to expect and what must be supplies) 2) Describe how code works 3) Do not use attribute names unless meaningful

Precondition Publish preconditions so the caller won't call methods with bad parameters Preconditions

Method should let user know if condition is violated. Caution: Text might imply otherwise

Condition that is true after a method has completed The return value is computed correctly The object is in a certain state after the method call is completed Don't document trivial postconditions that repeat clause Postconditions

/** * precondition: Balance is available for an account amount >=0 * postcondition:The account balance is adjusted to reflect * the deposit. */ public void deposit (double amount) { System.out.println(amount); balance = balance + amount; }

Lab: Part 2 Enhance the BankAccount Class  Add preconditions for all methods  Add parameters to documentation as necessary  Add postconditions where applicable  Post Complete labs to DropBoxes located in September 19 th ANGEL Lesson Tab

 Chapter 10

 Inheritance : derive a new class from an existing one  Parent class, or superclass, or base class.  Thechild class or subclass.  Component hierarchy:

 is-a relationship between superclass and a subclass or base class

Deposit BankForm parent Is-a component of child

Business KMartMacys ServiceBusiness Kinkos RetailBusiness is-a

JAVA Example 1: class RetailBusiness extends Business Example 2: class CheckingAccount extends BankAccount { }

SavingsAccount object inherits the balance instance field from BankAccount, and gains one additional instance field: interestRate : Layout of a Subclass Object

public class BankAccount { private double balance; /** * PostCondition: A bank account with a zero balance. */ public BankAccount() { balance = 0; } initialBalance the initial balance PostCondition: A bank account with a given balance is constructed. */ public BankAccount(double initialBalance) { balance = initialBalance; } Continued Next Page

/** * PreCondition: a balance amount the amount to deposit PostCondition: Money is deposited into the bank account. */ public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } /** * Precondition: A balance amount the amount to withdraw PostCondition: Money is withdrawn from the bank account. */ public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; } /** PostCondition: Current balance of the bank account the current balance */ public double getBalance() { return balance; }} BankAccount Class Continued

package infsy535bankacctwithinheritance; public class SavingsAccount extends BankAccount { private double interestRate; rate the interest rate PostCondition: A bank account with a given interest rate. */ public SavingsAccount(double rate) { interestRate = rate; } /** PostCondition: Added the earned interest to the account balance. */ public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } } Savings Account