Final Jim Brucker.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Programming in Java CSCI-2220 Object Oriented Programming.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object Oriented Programming
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Written by: Dr. JJ Shepherd
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Design issues for Object-Oriented Languages
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Advanced Programming in Java
Advanced Programming in Java
3 Introduction to Classes and Objects.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Methods Attributes Method Modifiers ‘static’
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Pointers Revisited What is variable address, name, value?
Programming Language Concepts (CIS 635)
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
ATS Application Programming: Java Programming
Chapter 13 Abstract Classes and Interfaces
Object Oriented Programming
Dynamic Memory Allocation
Object Based Programming
Chapter 9 Inheritance and Polymorphism
Polymorphism.
CSC 113 Tutorial QUIZ I.
Class Inheritance (Cont.)
Object-Oriented Programming: Polymorphism
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Group Status Project Status.
More On Enumeration Types
Advanced Java Programming
Introduction to Objects
CS18000: Problem Solving and Object-Oriented Programming
Advanced Programming in Java
Java Inheritance.
Sampath Kumar S Assistant Professor, SECE
Simple Classes in Java CSCI 392 Classes – Part 1.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Threads in Java James Brucker.
Using threads for long running tasks.
OO Programming Concepts
Chapter 11 Inheritance and Encapsulation and Polymorphism
Corresponds with Chapter 5
Advanced Programming in Java
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Threads and concurrency / Safety
Presentation transcript:

Final Jim Brucker

final modifier public static final int MAX_LENGTH = 256; private final String name; public void public final String getName( ) {return name;} public final class Double extends Number A final variable cannot be modified after first assignment. A final parameter cannot be reassigned (*). A final method cannot be overridden in a subclass. A final class cannot have a subclass.

Define a constant Constants are usually static. Variable name is uppercase. static final double NANOSECONDS = 1.0E-9;

Define an unchangable attribute Use final for attributes that should not change after object is created. 1. Must assign a value in declaration or in constructor. 2. Use camelCase names. public class Purse { // purse capacity cannot be changed! private final int capacity; Purse(int capacity) { this.capacity = capacity; }

A parameter that should not change Use final to indicate that a parameter or local variable should not change. code safety safely share variable among threads allows some code optimization by compiler // Run a task in a separate thread public void runInBackground( final Runnable task) { Thread t = new Thread(task); t.start(); // start a separate thread // returns immediately, without waiting

"final" is sometimes required For anonymous classes and threads, "final" may be needed for variables from the surrounding scope that are used in anonymous class or thread. Example: method uses the parameter (message) to define an anonymous class. This won't compile in Java 7 (or before) unless message is "final". public Runnable makeMessageTask(String message) { Runnable task = new Runnable() { public void run(){ System.out.println(message); }}; return task; }

"final" applies to reference, but ... You can still call mutator methods that modify attributes of an object, even though the reference is final. You can modify elements of a "final" array, too. final Date birthday = new Date(100,Calendar.JULY,1); // Only the reference is final! Object can change. birthday.setMonth(Calendar.JANUARY); final String [] s = {"This", "is", "final", "!"}; // that's what you think... s[3] = "or maybe not."; // ERROR, s is final s = new String[10];

"final" method cannot be overridden A final method may not be overridden by a subclass. public class Person { private long id; public final long getId() { return id; } } public class Student extends Person { public long getId() { return studentId; }//ERROR

Why declare a method final? 1. Method contains essential logic that subclasses must not change. public class Object public final void notify( ) Wake up a thread that is waiting for this object. public final void wait( ) Wait for another thread to invoke notify( )

Why declare a method final? 2. Efficiency. final methods cannot be overridden, so they are not a target for polymorphism. Compiler can use static binding (*) of final methods, which is more efficient at run-time. (*) binding - connect a method name to the actual method code.

final class cannot have a subclass A final class may not have subclasses. Methods in a final class are implicitly final. Some final classes: String, StringBuilder, Double, Integer public class MyString extends String ERROR - String class is final

Why Declare a Class as final? 1. Class is essential to correct functioning of other parts of the API or (for Java) the JVM. We don't want a subclass to screw up the logic! Strings are essential to Java. Java has special handling of Strings. concatenation: "hello, " + user string pool for constants conversion of String to/from other types, such as Double.parseDouble("2.4")

Why Declare a Class as final? 2. Efficiency. All methods are final, therefore the compiler can perform static binding (no polymorphism) which results in faster run-time. String methods are invoked a lot. They need to be fast. StringBuilder is used a lot to build strings. Its methods need to be fast, too.