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.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Interfaces A Java interface is a collection
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Java Software Solutions
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Abstract Classes.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Multiple Choice Solutions True/False a c b e d   T F.
Object Oriented Design and UML
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Polymorphism & Interfaces
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright Curt Hill Variables What are they? Why do we need them?
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
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.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 5: Enhancing Classes
Interfaces.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Agenda Warmup AP Exam Review: Litvin A2
Interfaces Professor Evan Korth.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Programming Language Concepts (CIS 635)
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Interfaces.
Class Inheritance (Cont.)
Implementing Non-Static Features
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Chapter 5: Enhancing Classes
Unit 3 Test: Friday.
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Review of Previous Lesson
Review: libraries and packages
2009 Test Key.
Chap 2. Identifiers, Keywords, and Types
Final and Abstract Classes
Object-Oriented Design Part 2
Presentation transcript:

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?

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.

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);

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.

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

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

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

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

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

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 }

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

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

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

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.

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 291-293 from the Unit 3 folder. Answer the following questions. Save answers in a Word doc called p291-3Qs, in your Programs folder: p. 291-3 multiple choice #4, 6, 7, 9, 10 P. 293 true/false #9 – 10 2) Continue working on Battleship.