Agenda Warmup AP Exam Review: Litvin A2

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
Abstract Classes and Interfaces
Multiple Choice Solutions True/False a c b e d   T F.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Agenda Object Oriented Programming Reading: Chapter 14.
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Agenda Warmup Lesson 4.5 (Program Design & Analysis)
Inheritance Notes: Using Object Oriented Design
Advanced Programming in Java
Objects as a programming concept
Haskell Chapter 2.
Interfaces.
Inheritance and Polymorphism
Agenda Warmup Finish 2.4 Assignments
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Lesson 2: Building Blocks of Programming
Interface.
Java Programming Language
Interfaces.
Advanced Java Programming
Agenda Warmup Lesson 4.5 (Program Design & Analysis)
Unit 3 Test: Friday.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Handout-4b More on Classes
Chapter 14 Abstract Classes and Interfaces
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
CMPE212 – Reminders Assignment 3 due next Friday.
CIS 199 Final Review.
Chapter 8 Class Inheritance and Interfaces
Review of Previous Lesson
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Review: libraries and packages
2009 Test Key.
Agenda Warmup Lesson 2.2 (parameters, etc)
Subtype Substitution Principle
Classes and Objects CGS3416 Spring 2019.
Agenda Warmup Lesson 2.8 (Overloading constructors, etc)
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

Agenda Warmup AP Exam Review: Litvin A2 Lesson 4.1 (this keyword, abstract class) Independent Practice (4.1 Assignments) Time Permitting: AP Exam Review Closure Activity Students will be able to: Understand how the this keyword works See examples of the this keyword Understand the difference between an abstract class and an interface Know when to use an abstract class vs an interface See how today's lesson fits into the unit and the course as a whole

Warmup What is dynamic binding? When does dynamic binding occur? What is one purpose for using polymorphism? What is an interface? What is the point of an interface? Can you instantiate an interface? Can a class implement an interface and inherit from a parent class? What is recursion?

this In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or constructor is being called. Example: public class Chess { // more code up here public boolean capture (Piece x) if (x.equals(this)) return false; }

Another this example public class Sample { private int x = 0; private int y = 0; public Sample (int x, int y) { this.x = x; this.y = y; }

More this examples ThisDemo, ThisDemoClient ThisDemo2, Hello

So, when/why would you use this? There are 3 main reasons: To specify that you are referring to an instance variable, when there is a local variable (or a static variable) of the same name 2) To refer to the object as a whole, such as: calling the toString method from within the same class sending the object (to another method) as a parameter returning the object from a method 3) To call other (overloaded) constructors within the class

Abstract Class We know what an interface is – a class that contains abstract (empty) methods, and can also contain constants. An abstract class is similar, but one major difference is that it can also contain methods that actually contain code. In other words, an abstract class can implement some of its methods, while an interface cannot. Like an interface, an abstract class CANNOT be instantiated. (In other words, you cannot, in a client, create an object of an abstract class.)

public abstract class Sample2 { public int sum (int num1, int num2) return (num1 + num2) } public char something(int z); /* note that this abstract class contains a method with actual code in its body, which is not allowed in an interface. */

Interface vs. Abstract Class You cannot instantiate (create an object of) an abstract class, but you can extend an abstract class, and then use its methods. So, what’s the point of an abstract class? Why not just use an interface, if you want to create a “blueprint” that programmers must follow? The answer is: If a bunch of programmers will be using the same method, in the same way, then it makes more sense to put this method in an abstract class and have them extend this class. But, if those programmers will be using the same method signature, but writing code for the method body in different ways, then it makes more sense to put that method in an interface and have them implement that interface.

We learned previously that all methods and variables in an interface must be public. (it wouldn’t make sense to have a private method in an interface, because where would this empty method be called from?) An abstract class, however, can have private methods and variables.

Assignment: Hangman 2-player game; player 1 enters the word while player 2 looks away, then player 2 guesses Display which letters have been guessed; error check -- don’t let user guess the same letter twice Always give player the choice of guessing a letter or guessing the word 9 wrong guesses = game over Allow for multiple games HINT: char array HINT: String concatenation