Recitation 9 October 28, 2011.

Slides:



Advertisements
Similar presentations
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
UML Class Diagram: class Rectangle
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
Inheritance using Java
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Polymorphism & Interfaces
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Click to edit Master text styles Stacks Data Structure.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Class Inheritance Part I
Abstract classes and interfaces
Advanced Programming in Java
Advanced Programming in Java
OOP: Encapsulation &Abstraction
Abstract Classes and Interfaces
Abstract Classes and Interfaces
University of Central Florida COP 3330 Object Oriented Programming
Inheritance and Polymorphism
Interfaces Professor Evan Korth.
Week 4 Object-Oriented Programming (1): Inheritance
UML Class Diagram: class Rectangle
Nested class.
Object Oriented Programming
Chapter 19 Generics Dr. Clincy - Lecture.
Abstract classes and interfaces
Inheritance, Polymorphism, and Interfaces. Oh My
Computers & Programming Languages
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Abstract Classes Page
Advanced Programming in Java
Recitation 7 October 7, 2011.
Shapes.
Java Inheritance.
Advanced Programming in Java
Recitation 6 September 30, 2011.
Based on slides by Alyssa Harding & Marty Stepp
Abstract classes and interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Presentation transcript:

Recitation 9 October 28, 2011

Today’s Goals: Review abstract classes Briefly review Java’s Scanner class Get more practice with scanning

Abstract classes An abstract class is a hybrid of a class and an interface Abstract classes are defined by including the word “abstract” before the word “class”:

Abstract classes: Like classes Has exactly one superclass (Object if none specified) Superclass can be either abstract OR non-abstract Can implement zero or more interfaces Can contain constructors; these constructors can call super constructors Can contain variables, constants, and implement methods

Abstract classes: Like classes Note the use of “this” to call constructor in same class. Call to super() is redundant, and would be included anyway. This is why the empty constructor was necessary on the test.

Abstract classes: Like interfaces Can’t create an instance of an abstract class (even if it contains constructors!) Illegal: Can implement interfaces but not provide implementations for the methods in those interfaces Legal:

Abstract classes: Unlike classes or interfaces Can define abstract methods (i.e. methods without implementations) and call them (!)

Non-abstract classes Also called “concrete” classes If a class is non-abstract, then it provides implementations of every unimplemented abstract method from all of its superclasses, as well as all methods from all interfaces implemented by all superclasses

Benefits of abstract classes Similar to benefits of inheritance Avoid repeating code Allow common behaviors Defer implementation to subclasses Indicate behavior must be provided, but is subclass-specific Prevents the creation of incomplete objects

Using abstraction

Using abstraction

Using abstraction In the code I provide, I also included a ResizableCartesianShape since it is difficult to resize triangles.

Review: java.util.Scanner import java.util.Scanner; Create a scanner object to read input from the console: Scanner scanner = new Scanner(System.in); Note: This reads console input, not program argument input To get the next string entered by the user (up to the first whitespace): scanner.next() To get the next integer entered by the user: scanner.nextInt() Show “guess what number I’m thinking” example.

Recitation Specification Download Recitation9.zip from the Recitations page Repeatedly read the next string entered by the user (scanner.next()) If the user enters “size”, print the size of the stack to the console If the user enters “pop”, pop the top element off of the stack If the user enters “triangle”, read in the following 6 integers (using scanner.nextInt()), create a CartesianTriangle object with these values, and push it onto the stack Remember, you need to manually refresh Object Editor to see these changes (View -> Refresh) Bonus (for candy): Allow the user to push as many of these as possible: lines, circles, ovals, rectangles, squares (all these classes are provided in graphics)