Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2

3 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. Strings, on the other hand, are not primitive: there is a class called String that has methods, such as .length( ), .charAt( ), etc. So, when you create a String variable, you are actually creating an object of the String class. Using primitive types is more efficient, in terms of code and computer memory. But what if you want to use certain methods on an int, a double, or a char?

4 Wrapper Classes A wrapper class allows you to “wrap” or “box” a primitive type into an object, so that you can use methods associated with that object. Example: Integer age = new Integer(34); The value of age is 34, but you can do more with this than you could with a normal, primitive int.

5 What is the point of a Wrapper Class?
The 3 most common uses of a wrapper class are: To use in an ArrayList (we’ll learn about ArrayLists soon – for now, you should know that they hold Objects, not primitive types. So, you need wrapper classes [which are subclasses of Object] when using an ArrayList. ) When you want to use a null reference (to deliberately set a variable value to null. When would you do this? One example: When you can’t be 100% sure that a method will return a valid integer.) When you want to use an Integer, Double, or Char polymorphically example: Object num = new Integer(23);

6 Interfaces An abstract method is a method with a first line (also called a signature or a header), but without a body An interface is a collection of abstract methods and (sometimes) constants An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish, as a “formal contract”, a set of methods that a class will implement. In simple terms: an interface is a blueprint/template/protocol that shows programmers how to write a certain type of class.

7 interface is a reserved word
public interface Sample { public final double PI = 3.14; public void times2(int x); public double getPI(); } An interface can contain constants Notice: none of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

8 in Sample must be implemented (given a definition)
public class Example implements Sample { public void times2(int x) System.out.println(x*2); } public double getPI() return PI; Each method listed in Sample must be implemented (given a definition) Example has access to the constant PI (from Sample) Open Sample, Example

9 In a class diagram, a dotted arrow points from any class that implements an interface to the interface. Example:

10 A class formally implements an interface by
An interface cannot be instantiated (meaning you can’t create an object of it from a client program). Methods in an interface cannot be private. It is nonsensical to have a private method in an interface – where would this method be called from? Remember, all methods in an interface are abstract (empty). A class formally implements an interface by stating so in the class header (using the word implements) providing implementations (writing code) for each abstract method in the interface If a class implements an interface, then it MUST define/implement (provide code for) all methods that are in the interface

11 A class that implements an interface can contain other methods as well
In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

12 The interfaces are listed in the implements clause
A class can implement multiple interfaces. (Note that this is different from inheritance: a class can only extend (inherit from) ONE parent.) The interfaces are listed in the implements clause The class MUST implement (write code for) all methods in all interfaces listed in the header Also, many different classes can implement the same interface. public class Mult implements Interface1, Interface2 { // all methods of both interfaces // MUST be defined here }

13 Note: extends must occur before implements
It is possible for a class to both extend a parent class and implement interfaces: public class Car extends Vehicle implements Interface1 { } Note: extends must occur before implements

14 The Java standard class library contains many helpful interfaces
The Comparable interface contains an abstract method called compareTo, which is used to compare two objects lexicographically (comparing their ASCII values). The String class implements Comparable, giving us the ability to put strings in lexicographic order: compares the letters using their ASCII (aka Unicode) values Demo: Compare

15 When a programmer writes a class that implements the Comparable interface, it’s up to him/her to determine what makes one object greater or less than another Not all objects are compared numerically or lexicographically

16 Confusing terminology:
A class that “connects” to an interface is said to implement the interface, by using the keyword implements Sometimes, you will see exams and text books mention that a certain method has not been implemented – what they mean is that the method has not been given any code in its body. In other words, the method has a signature, but is otherwise empty.

17

18

19

20

21 Assignments 1) Everyone - open “Litvin Practice Test A2” in the “AP Practice Exams” folder. Only do the problems listed on the board. Write down all answers (and work if necessary) on paper. Hand in when done. 2) Open pages from the Unit 3 folder. Answer the following questions. Save answers in a Word doc called p291-3Qs, in your Programs folder: p multiple choice #4, 6, 7, 9, 10 P. 293 true/false #9 – 10 2) Continue working on Battleship.


Download ppt "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."

Similar presentations


Ads by Google