Programming Language Concepts (CIS 635)
Elsa L Gunter
4303 GITC
NJIT, www.cs.njit.edu/~elsa/635
Copyright 2002 Elsa L. Gunter
Static Binding in Java
• Modifier final used to make a method (or variable) be statically bound, instead of dynamically bound
• When used on variable (or member), variable becomes a constant
• Final methods cannot be overridden in any subclass
• Final class has no subclasses
Copyright 2002 Elsa L. Gunter
Dynamic Binding in C++ versus Java
• Dynamic binding default in Java
• Static binding default in C++
• In C++ use virtual keyword on method to cause it to be dynamically bound
Copyright 2002 Elsa L. Gunter
Abstract Classes in C++
• A purely virtual method is used for methods that are not to be implemented in base class
• Indicated by giving a body of 0• Purely virtual methods must be
overridden in subclasses• Class is abstract if it contains purely
virtual functions
Copyright 2002 Elsa L. Gunter
Abstract Classes in Java
• Abstract class has modifier abstract
• May contain abstract methods, prefixed by modifier abstract, and having no body
• Abstract classes have no instance objects
• Subclass must either override all abstract methods, or be abstract itself
Copyright 2002 Elsa L. Gunter
Abstract Classes in Java – Example
• Before we had :public class CustomerAccount {… protected int fee () {return 0;} protected int interest () {return 0;}
…}• Want to force fee and interest to be
overridden
Copyright 2002 Elsa L. Gunter
Abstract Classes in Java – Example
• Use abstract methods (and class)
abstract public class CustomerAccount {
…
abstract protected int fee ();
abstract protected int interest ();
…
}
Copyright 2002 Elsa L. Gunter
Abstract Classes in Java – Example
• Can use ChargeAccount just as before
• Credit account must supply a fee method
Copyright 2002 Elsa L. Gunter
Interfaces in Java
• Abstract class specification• Defined with keyword interface
instead of class• Purpose to supply export
information• All methods are public and abstract
(public and abstract keywords not used)
Copyright 2002 Elsa L. Gunter
Interfaces in Java
• Class may implement several interfaces, but my extend only one class
• Parameter to method may have type of an interface
• Object from any class that implements an interface may be used– Class that implements an interface is
a subtype of interface
Copyright 2002 Elsa L. Gunter
Packages in Java• Package is a collection of classes• Intent: classes in package work
together• If a member of a class is not
marked public, private, or protected, then it is visible to all other classes in package, but not outside
Copyright 2002 Elsa L. Gunter
Packages in Java• Class is added to package by being
preceded by directivepackage <module name>;at start of source file
• Access classes by <module name>.<class name>
• All classes member of some package (noname package, if none specified)
Copyright 2002 Elsa L. Gunter
Java Class IOTest• Modified from “Learn Java Now” ny
Stephen R. Davis, Microsoft Pressimport java.io.*;public class IOTest {
public static void main(String args[]) { try { //Read input from the keyboard byte bArray[] = new byte[128]; System.out.println("Enter something:");
Copyright 2002 Elsa L. Gunter
Java Class IOTest // Reads in an array of bytes
System.in.read(bArray);
// Convert array into a String before // outputting it String s = new String(bArray, 0); System.out.println("You entered: " + s); }
Copyright 2002 Elsa L. Gunter
Java Class IOTest catch(IOException ioe)
{ System.out.println(ioe.toString()); ioe.printStackTrace(); } }}
Copyright 2002 Elsa L. Gunter
Example
• What is the result for:
3 + 4 * 5 + 6
• Possible answers:– 41 = ((3 + 4) * 5) + 6
– 47 = 3 + (4 * (5 + 6))– 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6)– 77 = (3 + 4) * (5 + 6)
Copyright 2002 Elsa L. Gunter
Operator Precedence
• Operators of highest precedence evaluated first (bind more tightly).
• Precedence for infix binary operators given in following table
Copyright 2002 Elsa L. Gunter
Example
• If assignment returns the assigned value, what is the result of
x = 5;
y = (x = 3) + x;
Copyright 2002 Elsa L. Gunter
Example
• If assignment returns the assigned value, what is the result of
x = 5;y = (x = 3) + x;
• Possible answers: 6 or 8• Depends on language, and sometimes
compiler– C allows compiler to decide– SML forces left-to-right evaluation
Copyright 2002 Elsa L. Gunter
Control Structures
• Expression Evaluation– Determined by operator evaluation
order and operand evaluation order
• Operators:– Most operators are either infix or
prefix (some languages have postfix)
– Order of evaluation determined by operator precedence and associativity
Copyright 2002 Elsa L. Gunter
Example Again
• In most language, 3 + 4 * 5 + 6 = 29
• In APL, all infix operators have same precedence– Thus we still don’t know what the
value is
Copyright 2002 Elsa L. Gunter
Example
• What is the value of:
7 – 5 – 2
• Possible answers:– In Pascal, C++, SML assoc. left
7 – 5 – 2 = (7 – 5) – 2 = 0
– In APL, associate to right
7 – 5 – 2 = 7 – (5 – 2) = 4
Copyright 2002 Elsa L. Gunter
Special Associativity
• In languages with built in support for infix exponent operator, it is standard for it to associate to the right:
2 ** 3 ** 4 = 2 ** (3 ** 4)
• In ADA, exponentiation in non-associative; must use parentheses
Copyright 2002 Elsa L. Gunter
Operand Evaluation Order
• Example:
A := 5;
f(x) = {A := x+x; return x};
B := A + f(A);
• What is the value of B?
• 10 or 15?
Copyright 2002 Elsa L. Gunter
Solution to Operand Evaluation Order
• Disallow all side-effect– “Purely” functional languages try to do
this – Miranda, Haskell
– Problem: I/O, error conditions such as overflow are inherently side-effecting
Copyright 2002 Elsa L. Gunter
Solution to Operand Evaluation Order
• Disallow all side-effects in expressions but allow in statements– Problem: not applicable in languages
with nesting of expressions and statements
Copyright 2002 Elsa L. Gunter
Solution to Operand Evaluation Order
• Fix order of evaluation– SML does this – left to right
– Problem: makes some compiler optimizations hard to impossible
• Leave it to the programmer to be sure the order doesn’t matter– Problem: error prone
Copyright 2002 Elsa L. Gunter
Short-circuit Evaluation
• Boolean expressions:
• Example: x <> 0 andalso y/x > 1
• Problem: if andalso is ordinary operator and both arguments must be evaluated, then y/x will raise an error when x = 0
Copyright 2002 Elsa L. Gunter
Boolean Expressions
• Most languages allow (some version of) if…then…else, andalso, orelse not to evaluate all the arguments
• if true then A else B–doesn’t evaluate B
Copyright 2002 Elsa L. Gunter
Boolean Expressions
• if false then A else B–doesn’t evaluate A
• if b_exp then A else B–Evaluates b_exp, then applies
previous rules
Copyright 2002 Elsa L. Gunter
Boolen Expressions
• Bexp1 andalso Bexp2– If Bexp1 evaluates to false, doesn’t
evaluate Bexp2
• Bexp1 orelse Bexp2– If Bexp1 evaluates to true, doesn’t
evaluate Bexp2
Copyright 2002 Elsa L. Gunter
Short-circuit Evaluation – Other Expressions
• Example: 0 * A = 0• Do we need to evaluate A?• In general, in f(x,y,…,z) are the
arguments to f evaluated before f is called and the values are passed, or are the unevaluated expressions passed as arguments to f allowing f to decide which arguments to evaluate and in which order?
Copyright 2002 Elsa L. Gunter
Eager Evaluation
• If language requires all arguments to be evaluated before function is called, language does eager evaluation and the arguments are passed using pass by value (also called call by value) or pass by reference
Copyright 2002 Elsa L. Gunter
Lazy Evaluation
• If language allows function to determine which arguments to evaluate and in which order, language does lazy evaluation and the arguments are passed using pass by name (also called call by name)
Top Related