Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Choice Solutions True/False a c b e d   T F.

Similar presentations


Presentation on theme: "Multiple Choice Solutions True/False a c b e d   T F."— Presentation transcript:

1 Multiple Choice Solutions True/False a c b e d T F

2 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; }

3 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.

4 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.

5 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 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.)

6 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. */

7 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.

8 We learned yesterday 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.

9 for-each loop When going through (“traversing”) every element in an array or ArrayList, an efficient way to do so is by using a for-each loop. Syntax: for (type element : array) { // body of loop }

10 Using for-each loops with 2D arrays
Remember, a 2D array is really just a 1D array in which each element is itself a 1D array. This is important, because when using a for-each loop with a 2D array, you first place each row of the table into a 1D array. Then, with a nested for-each loop, you assign each element of each row into that 1D array. Demo: ForEachLoopDemo

11 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


Download ppt "Multiple Choice Solutions True/False a c b e d   T F."

Similar presentations


Ads by Google