Download presentation
Presentation is loading. Please wait.
1
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
2
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?
4
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; }
5
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; }
6
More this examples ThisDemo, ThisDemoClient ThisDemo2, Hello
7
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
8
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.)
9
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. */
10
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.
11
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.
12
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.