Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
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.
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.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
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 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
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.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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 11: Subclasses Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Subclasses.
Chapter 11 Subclasses.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Introduction to Object-Oriented Programming
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 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.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Topic 4 Inheritance.
 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 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
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.
Java Object Oriented Programming Encapsulation, Inheritance, Polymorphism (Java: An Eventful Approach - Ch. 17, 21.7), Slides Credit: Bruce, Danyluk and.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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 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 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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
BY:- TOPS Technologies
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance In the real world, objects aren’t usually one-of-a-kind.
Inheritance and Polymorphism
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
ATS Application Programming: Java Programming
CSC 205 Java Programming II
CSC 205 – Java Programming II
CSC 205 Programming II Lecture 2 Subclassing.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Week 6 Object-Oriented Programming (2): Polymorphism
CSC 205 – Java Programming II
Presentation transcript:

Some Important topics

Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file

Writing to Files Creating a FileOutputStream object opens a binary file for writing: FileOutputStream out = new FileOutputStream( filename ); Opening a file for input (Read ) will fail—causing an exception to be thrown—unless the file already exists. Attempting to open a nonexistent file for output (Write) will cause the file to be created. Either way, the file is empty to begin with.

Working with Files One of these methods, named write, is capable of writing a single byte: out.write(b); // Writes the byte b A file should be closed when no more operations will be performed on it. Closing a file is done by calling the close method: out.close();

Reading from Files FileInputStream file_in = new FileInputStream(filename) BufferedReader in = new BufferedReader(new InputStreamReader(file_in)) A loop that reads all the bytes in a file: while (true) { int byteValue = in.read(); if (byteValue == -1) break; // End of file reached byte inputByte = (byte) byteValue; … }

Copyright © 2000 W. W. Norton & Company. All rights reserved. 6 Subclasses

Copyright © 2000 W. W. Norton & Company. All rights reserved. 7 Inheritance In the real world, objects aren’t usually one- of-a-kind. Both cars and trucks are examples of vehicles. A car is a special type of vehicle; so is a truck. In Java, relationships such as the one between cars and vehicles are expressed using a feature known as inheritance. Inheritance allows a class to have a “parent” from which it inherits some of its state and some of its behavior.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 8 Advantages of Inheritance There would be no point for a car designer to start from scratch when designing a new car. By assuming that a car has certain components and certain behaviors, a car designer can focus on what distinguishes the new model from older ones. Inheritance gives programmers a similar advantage. A new class created from an existing class inherits its variables and methods. The programmer can declare additional variables and methods to make the new class unique.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 9 Class Extension In Java, inheritance is accomplished by extending an existing class. Extending a class is done by putting the word extends in a class declaration, followed by the name of the class that’s being extended: public class Car extends Vehicle { … } Car is said to be a subclass of Vehicle, and Vehicle is said to be the superclass of Car.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 10 Class Extension A subclass inherits the variables and methods of its superclass, with the exception of constructors, private variables, and private methods. Inherited variables and methods behave as though they were declared in the subclass. The subclass may define additional variables and methods that were not present in the superclass.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 11 Writing a Subclass A superclass can be any previously existing class, including a class in the Java API. The Account class of Section 3.3 can be extended to create a new SavingsAccount class. If different savings accounts can have different interest rates, each SavingsAccount object will need to store an interest rate.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 12 Writing a Subclass A preliminary version of the SavingsAccount class: public class SavingsAccount extends Account { private double interestRate; public double getInterestRate() { return interestRate; } public void setInterestRate(double rate) { interestRate = rate; }

Copyright © 2000 W. W. Norton & Company. All rights reserved. 13 Writing a Subclass An instance of a subclass stores all the instance variables of the superclass, plus all the instance variables defined in the subclass.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 14 Writing a Subclass A SavingsAccount object will contain two variables ( balance and interestRate ):

Copyright © 2000 W. W. Norton & Company. All rights reserved. 15 Writing a Subclass Methods that can be applied to SavingsAccount objects: – getInterestRate – setInterestRate – deposit (inherited) – withdraw (inherited) – getBalance (inherited) – close (inherited)

Copyright © 2000 W. W. Norton & Company. All rights reserved. 16 Writing a Subclass If savingsAcct is a SavingsAccount variable, the following statements are all legal: System.out.println("Rate: " + savingsAcct.getInterestRate()); savingsAcct.setInterestRate(4.25); savingsAcct.deposit(500.00); savingsAcct.withdraw(100.00); System.out.println("Balance: " + savingsAcct.getBalance()); savingsAcct.close();

Copyright © 2000 W. W. Norton & Company. All rights reserved. 17 Writing a Subclass When an instance method is called, Java looks first in the class to which the calling object belongs, then in the class’s superclass, then in the superclass of that class, and so on. Consider the following statement: savingsAcct.deposit(500.00); Java first looks for the deposit method in the SavingsAccount class, then in the Account class.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 18 Writing Subclass Constructors A subclass doesn’t inherit constructors from its superclass, so it will need its own constructors. The hard part of writing a constructor for a subclass is initializing the variables that belong to the superclass, which are likely to be private. The constructor for the SavingsAccount class will need to initialize both the balance and interestRate variables.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 19 Writing Subclass Constructors A first attempt at writing the constructor: public SavingsAccount(double initialBalance, double initialRate) { balance = initialBalance; // WRONG; balance is private interestRate = initialRate; } This version of the constructor won’t compile, because balance was declared private in the Account class.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 20 Writing Subclass Constructors There are two ways to solve this problem. One is for the SavingsAccount constructor to invoke the Account constructor by using the word super : public SavingsAccount(double initialBalance, double initialRate) { super(initialBalance); // Invoke Account constructor interestRate = initialRate; } super must come first, before the other statements in the body of the subclass constructor.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 21 The protected Access Modifier public and private are the primary access modifiers in Java: – Declaring a variable or method to be public allows universal access to that variable or method. – Declaring a variable or method to be private limits access to the class in which the variable or method is defined. The public/private distinction is sometimes too restrictive, because it doesn’t distinguish a subclass from classes in general.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 22 Differences between Access Modifiers A summary of the differences between private, protected, and public when applied to a variable or method in a class: Access Modifier Meaning private Can be accessed only in the same class protected Can be accessed in the same class or in a subclass public Can be accessed in any class None Can be accessed in any class in the same package A class also has access to the protected variables and methods of all classes in the same package.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 23 Polymorphism The exact meaning of each instruction may vary, depending on the vehicle: – For a car, “accelerate” means “press on the accelerator.” – For a bicycle, “accelerate” means “turn the pedals.” In object-oriented programming, polymorphism refers to the ability of different kinds of objects to respond differently to the same commands, provided that the objects belong to classes with a common ancestor.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 24 The Substitution Principle An example that uses the Substitution Principle: Vehicle v = new Car(); Although this statement looks wrong, it is valid as long as Vehicle is a superclass of Car. A car is a vehicle: it has all the properties and behaviors of a vehicle. Some behaviors were inherited and left unchanged, while others were overridden, but—either way—they’re guaranteed to exist.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 25 A Polymorphic Algorithm in Java A Java version of the algorithm for operating a vehicle: v.start(); v.releaseBrake(); v.accelerate(); v.applyBrake(); v.stop(); It doesn’t matter what v is, as long as it’s some kind of vehicle. This algorithm is polymorphic: it works for a variety of vehicles, not a single kind.

Copyright © 2000 W. W. Norton & Company. All rights reserved. 26 Dynamic Binding In a call such as v.start(), the compiler can’t determine which version of start is being called; instead, v will have to be tested during program execution. This process is known as dynamic binding, because the exact method that’s being called won’t be known until the program is run. If different objects are assigned to v during execution, different versions of start may be called: v = new Car(); v.start(); // Calls start method in Car class v = new Truck(); v.start(); // Calls start method in Truck class