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.

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

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
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.
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.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
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. 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.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
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.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
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 Motivation –Code reuse –Conceptual modeling.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
 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.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
1 Principles of Computer Science II Prof. Nadeem Abdul Hamid CSC 121 – Spring 2006 Lecture Unit 4 - Inheritance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Chapter 13 - Inheritance.
Big Java Chapters 9-10.
Lecture 12 Inheritance.
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Computing with C# and the .NET Framework
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
Polymorphism and access control
Agenda About Homework for BPJ lesson 36 About practice of last class
Adapted from Java Concepts Companion Slides
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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 your age"); int age = scan.nextInt(); // Better to be safe System.out.println("Enter your school grade"); String gradeStr = scan.next(); // Exceptions covered in more detail later try { int grade = Integer.parseInt(gradeStr); } catch (NumberFormatException e) { System.out.println("That's not a number!"); }

INHERITANCE More OO Concepts

Inheritance Topics Basic Inheritance –BankAccount & SavingsAccount –extends, super, equals –access control Typecast, instanceof Polymorphism Abstract Classes

Inheritance Basics Models “is-a” relationship, same as C++ requires extends keyword All classes are children of class Object Object has several methods that are frequently overridden: –toString: returns a string that describes the state of the object, automatically called by System.out.println(objectRef); –clone: creates a deep copy of an object –equals: does a field-by-field comparison, returns boolean result –hashcode: if object will be used as a hash key QUICK EXERCISE: draw deep vs shallow copy

Simple Inheritance Example public class BankAccount { public BankAccount() { super(); // call superclass constructor - Object balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } public void deposit(double amount) { double newBalance = balance + amount; if (newBalance >= 0) // we’ll cover error handling later balance = newBalance; } public void withdraw(double amount) { double newBalance = balance - amount; if (newBalance >= 0) balance = newBalance; } public double getBalance() { return balance; } private double balance; } could be protected, but easier maintenance if private

Simple Inheritance Example class SavingsAccount extends BankAccount { public SavingsAccount(double balance, double rate) { super(balance); // calls parent constructor interestRate = rate; } void addInterest() { // use getters/setters unless protected double interest = getBalance()* interestRate / 100; deposit(interest); } private double interestRate; } can’t directly update balance if private

Another use of super In BankAccount public String toString() { // note use of Double wrapper class method return Double.toString(balance); // could do return "Balance: " + balance; // another option is: // return getClass().getName() + balance; } In SavingsAccount public String toString() { return super.toString() + "Interest rate " + interestRate; } calls parent class method class object describes class & properties

More on Inheritance SavingsAccount collegeFund = new SavingsAccount(1000, 0.1); // parent variable can “point” to child object BankAccount anAccount = collegeFund; // all objects are children of Object Object anObject = collegeFund; collegeFund.addInterest(); // OK anAccount.addInterest(); //not OK

Typecasts If you know that a reference contains a specific type of object, you can perform a cast (will be an exception if you’re wrong) BankAccount myAccount = (BankAccount) anObject; Can use instanceof to test class type* if (anObject instanceof BankAccount) { BankAccount myAccount = (BankAccount) anObject; … } *will be used very sparingly with good object-oriented design – rely on polymorphism instead. instanceof includes subclass, getClass does not.

Object Method Examples – equals public boolean equals(Object otherObject) { if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; BankAccount account = (BankAccount) otherObject; return balance == account.balance; // should call super.equals first if you write an // equals for a subclass // instanceof sometimes used instead of // getClass(), but it would return true for a // subclass } What happens if you don’t override equals? You should override hashCode too… use Eclipse for this.

Polymorphism In Java, method calls are always determined by the type of the actual object, not the type of the object reference (unlike C++, where virtual keyword is needed)

Access control public – like C++, good for constants private – like C++, good for instance fields protected – like C++ but extends to package, convenient for inheritance package –all methods of classes in the same package have access –this is the default! (easy to forget) –OK if several classes in package collaborate, but generally inner classes are better, packages are not secure (anyone can add a class to the package) public class BankAccount { double balance; // package access... }

Access Control - Summary PublicPackageProtectedPrivate Classes outside Package X Other class in Package XX SubclassXXX This classXXXX

Abstract Classes Concept same as C++, use in same types of situations (e.g., force subclasses to define behavior) Requires use of abstract keyword As in C++: –you can’t initialize objects of an abstract type. –you can initialize a variable of an abstract class with a subclass Abstract classes can include: –variables –constants –function definitions (e.g., access common variables) –function declarations (no function body) (soon we’ll learn about Interfaces, which have only constants and abstract functions (no function definitions))

Abstract Example abstract public class ChessPiece { private int row; private int col; // would have other fields public ChessPiece(int row, int col) { super(); this.row = row; this.col = col; } // Must be defined in subclass abstract public void move(); } public class Queen extends ChessPiece { public Queen(int row, int col) { super(row, col); public void move() { // Code here to move diagonally, horizontally, vertically }

Final Methods and Classes – just fyi, not covered May occasionally want to prevent others from creating subclasses or overriding methods public final class String {... } public final boolean checkPassword(String password) {... }