Download presentation
Presentation is loading. Please wait.
Published byAlban Davidson Modified over 8 years ago
1
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 void capture (Piece x) { if (x.equals(this)) return false; }
2
Another this example public class Sample { public int x = 0; public int y = 0; public Sample (int x, int y) { this.x = x; this.y = y; } // now, there are local copies of the parameters x and y in this class.
3
The List Interface In addition to the Comparable interface, another interface that you must be aware of is the List interface. ArrayList is a class that implements List. We will soon learn about polymorphism, which is used when a parent class or interface has a method that gets overridden by its child/subclass. For now, all you need to know about List is that it’s an interface that gets implemented by ArrayList.
4
Abstract Class We know what an interface is – a class that contains abstract (empty) methods, and can also contain constants. can An abstract class is similar, but one major difference is that it can contain methods that actually contain code. – In other words, an abstract class can implement 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.)
5
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. */
6
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.
7
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.
8
Static Methods A static method is a method that can be called by using the name of the class itself. You do not need to create an object in order to call the method. We have been using static methods for a long time: SavitchIn.readLine(); Math.pow(), etc. This explains why you don’t have to “connect” (create an object of) the SavitchIn class in order to use it. Open: StaticMethodClass, StaticMethodClient
9
Assignment Continue working on Independent Projects.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.