“The task of leadership is not to put greatness into people, but to elicit it, for the greatness is there already.” – John Buchan Thought for the Day.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
March 2004Object Oriented Design1 Object-Oriented Design.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
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.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
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.
CHAPTER 10 OBJECT INHERITANCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
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.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Two Ways to Store Data in a File Text format Binary format.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003.
The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming.
© S Ramakrishnan, Monash University oops1.ppt 1 Object-Oriented Programming Systems SFT3021 Semester Lecturer: Sita Ramakrishnan
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
 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.
Introduction IS Outline  Goals of the course  Course organization  Java command line  Object-oriented programming  File I/O.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Topic 4 Inheritance.
“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Inheritance and Polymorphism Lecture 3. C# Classes  Classes are used to accomplish:  Modularity: Scope for methods  Blueprints for generating objects.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the.
Data Structures and Algorithms revision
Lecture 12 Inheritance.
Lecture Notes – Inheritance (Ch 9-10)
Computing with C# and the .NET Framework
CS100A Lecture 22 Previous Lecture This Lecture Inheritance
CSC 205 Java Programming II
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Object-Oriented Programming
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism
Introduction to Inheritance
Advanced Inheritance Concepts
Chengyu Sun California State University, Los Angeles
Presentation transcript:

“The task of leadership is not to put greatness into people, but to elicit it, for the greatness is there already.” – John Buchan Thought for the Day

Reading from Files Use a FileReader BufferedReader in = new BufferedReader(new FileReader("f.txt")); String s; int total = 0; while ((s = in.readLine()) != null) { total += Integer.parseInt(s); } in.close(); System.out.println("Total = " + total);

Writing to Disk Files Use a FileWriter Methods to write strings and characters

Going Further Much more to I/O in Java –Binary data files –Random access files –Reading and writing objects (serialization) –Network I/O Even database access (JDBC)

Chapter 11 More Classes and Objects

Important Object-Oriented Concepts Inheritance Method overriding Polymorphism

Inheritance Often classes (or objects) have a lot in common –e.g. bank accounts Account balance accountNumber interestRate deposit, withdraw

Inheritance (cont.) Other types of accounts will be similar –e.g. cheque (current) account ChequeAccount balance, accountNumber interestRate overdraftLimit, overdraftIntRate deposit, withdraw

Problems Duplication of code and data Difficult maintenance –e.g. account number format changes More types of accounts –even more duplication

Inheritance — The Solution Classes can inherit properties from other classes –Like genetic inheritance In Java: public class X extends Y { } // class X In Python we would write: class X(Y):...

Inheritance (cont.) Diagrammatically: X... Y...

Inheritance (cont.) Cheque account class: public class ChequeAccount extends Account { public double overdraftLimit; public double overdraftIntRate; } // class ChequeAccount This class inherits: –balance, account number and interest rate –withdraw and deposit methods

Some Terminology Superclass (parent class) Subclass (child class) Subclassing (inheriting) In Java: only one superclass –single inheritance Y XA

Another Example Throwable ExceptionError RuntimeException IOException...

Overriding Methods Sometimes a subclass needs to do things a little differently to the superclass –e.g. withdrawals from a cheque account Method overriding –replaces inherited method –it is not the same as overloading

Overriding Methods (cont.) public class ChequeAccount extends Account { public double overdraftLimit; public double overdraftIntRate; public void withdraw (double amount) {... // Allow for overdraft } // withdraw } // class ChequeAccount

The Object Class Superclass of all Java classes public class X { } // class X extends Object

Object Methods All Java objects inherit some common methods from the Object class –toString –equals

The toString Method By default gives class name and memory address: But, we can override it! ChequeAccount cq = new ChequeAccount(); System.out.println(cq);

Overriding toString public class ChequeAccount extends Account {... public String toString () { return (accountNumber + ” bal = R” + balance); } // toString } // class ChequeAccount