Programming Languages Technology and Paradigms Basic Java | CMSC 433, Papers of Programming Languages

Material Type: Paper; Professor: Hicks; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Fall 2002;

Typology: Papers

Pre 2010

Uploaded on 07/30/2009

koofers-user-hfu
koofers-user-hfu 🇺🇸

9 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 433,Michael Hicks, U. Maryland
(via W. Pugh, A. Sussman, and A, Porter)
1
CMSC433, Fall 2002
Programming Language
Technology and Paradigms
Basic Java
Michael Hicks
Sep 10, 2002
CMCS 433, Fall 2002
-
Michael Hicks
28
Last Time
OO principles
Keys: abstraction, encapsulation, sharing
Java basics
Everything is an Object
Object “contract”
Downcasting
Objects are always referenced on the Heap
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Programming Languages Technology and Paradigms Basic Java | CMSC 433 and more Papers Programming Languages in PDF only on Docsity!

CMSC 433,Michael Hicks, U. Maryland

CMSC433, Fall 2002

Programming Language

Technology and Paradigms

Basic Java

Michael Hicks

Sep 10, 2002

CMCS 433, Fall 2002 - Michael Hicks 28

Last Time

  • OO principles
    • Keys: abstraction, encapsulation, sharing
  • Java basics
    • Everything is an Object
      • Object “contract”
      • Downcasting
    • Objects are always referenced on the Heap

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 29

Visibility Modifiers

  • Indicate visibility of
    • Classes
    • Methods
    • Fields
  • Support abstraction
    • Clients unaffected by change in implementation
  • Support encapsulation
    • Prevents leaking of information to clients

CMCS 433, Fall 2002 - Michael Hicks 30

Class modifiers

  • public – class visible outside package
  • final – no other class can extend this class
  • abstract – no instances of this class can be

created

  • only instances of extensions of the class
  • No modifier implies package -level scope

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 33

Instance vs. static

int foo

int foo

int foo

Foo int bar;

Public class Foo { int foo; static int bar; }

Class definition

Class implementation

Objects of class Foo

CMCS 433, Fall 2002 - Michael Hicks 34

Examples

  • public static void main(String args[]) { … }
  • public class Math {

public final static PI = 3.14159…;

  • public class System {

public static PrintStream out = …;

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 35

Instance variable modifiers

  • final – can’t be changed; must be initialized

in declaration or in constructor

  • transient, volatile
    • will cover later

CMCS 433, Fall 2002 - Michael Hicks 36

Method modifiers

  • final – this method cannot be overridden
    • useful for security
    • allows compiler to inline method
  • abstract – no implementation provided
    • class must be abstract
  • native, synchronized
    • will cover later

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 39

Overloading

  • Methods with the same name, but different

parameters (count or types) are overloaded

class Parent { int cost; void add (int x) { cost += x; } void add (String s) throws NumberFormatException { cost += Integer.parseInt(s); } }

CMCS 433, Fall 2002 - Michael Hicks 40

Dynamic Method Dispatch

  • If you have a ref a of type A to an object

that is actually of type B (a subclass of A )

  • instance methods invoked on a will get the methods for class B (like C++ virtual functions)
  • class methods invoked on a will get the methods for class A - invoking class methods on objects strongly discouraged

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 41

Simple Dynamic Dispatch Example

public class A { String f() {return “A.f() “; } static String g() {return “A.g() “; } } public class B extends A { String f() {return “B.f() “; } static String g() {return “B.g() “; } public static void main(String args[]) { A a = new B(); B b = new B(); System.out.println(a.f() + a.g() + b.f() + b.g()); } }

java B generates : B.f() A.g() B.f() B.g()

CMCS 433, Fall 2002 - Michael Hicks 42

Self reference

  • this refers to the object the method is

invoked on

  • super refers to the same object as this
    • but used to access methods/variables in superclass
  • Like C++

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 45

Detailed Example

  • Shows
    • polymorphism for both method receiver and arguments
    • static vs. instance methods
    • overriding instance variables

CMCS 433, Fall 2002 - Michael Hicks 46

Source code for classes

class A { String f(A x) { return “A.f(A) “; } String f(B x) { return “A.f(B) “; } static String g(A x) { return “A.g(A) “; } static String g(B x) { return “A.g(B) “; } String h = “A.h”; String getH() { return “A.getH(): ” + h; } } class B extends A { String f(A x) { return “B.f(A)/ “ + super.f(x); } String f(B x) { return “B.f(B)/ “ + super.f(x); } static String g(A x) { return “B.g(A) “; } static String g(B x) { return “B.g(B) “; } String h = “B.h”; String getH() { return “B.getH(): ” + h + “/” + super.h; } }

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 47

A a = new A(); A ab = new B(); B b = new B(); System.out.println( a.f(a) + a.f(ab) + a.f(b) ); // A.f(A) A.f(A) A.f(B) System.out.println( ab.f(a) + ab.f(ab) + ab.f(b) ); // B.f(A)/A.f(A) B.f(A)/A.f(A) B.f(B)/A.f(B) System.out.println( b.f(a) + b.f(ab) + b.f(b) ); // B.f(A)/A.f(A) B.f(A)/A.f(A) B.f(B) A.f(B) System.out.println( a.g(a) + a.g(ab) + a.g(b) ); // A.g(A) A.g(A) A.g(B) System.out.println( ab.g(a) + ab.g(ab) + ab.g(b) ); // A.g(A) A.g(A) A.g(B) System.out.println( b.g(a) + b.g(ab) + b.g(b) ); // B.g(A) B.g(A) B.g(B) System.out.println( a.h + “ “ + a.getH() ); // A.h A.getH():A.h System.out.println( ab.h + “ “ + ab.getH() ); // A.h B.getH():B.h/A.h System.out.println( b.h + “ “ + b.getH() ); // B.h B.getH():B.h/A.h

Invocation

and results

CMCS 433, Fall 2002 - Michael Hicks 48

What to notice

  • Invoking ab.f(ab) invokes B.f(A)
    • run-time type of object determines method invoked
    • compile-time type of arguments used
  • ab.h gives the A version of h
  • ab.getH()
    • B.getH() method invoked
    • in B.getH() , h gives B version of h
  • Use of super in class B to reach A version of methods/variables
  • super not allowed in static methods

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 51

No multiple inheritance

  • A class type can be a subtype of many other

types ( implements )

  • But can only inherit method

implementations from one superclass

( extends )

  • Not a big deal
    • multiple inheritance rarely, if ever, necessary and often badly used
  • And it’s complicated to implement well

CMCS 433, Fall 2002 - Michael Hicks 52

Poor man’s polymorphism

  • Every object is an Object
  • Thus, a data structure Set that implements

sets of Object s

  • can summarily hold String s
  • or images
  • or … anything!
  • The trick is getting them back out:
  • When given an Object , you have to downcast it

CMSC 433,Michael Hicks, U. Maryland

CMCS 433, Fall 2002 - Michael Hicks 53

Example

class DumbSet { public void insert(Object o) {..} public bool member(Object o) {..}public Object any() {..} } class MyProgram { public static void main(String[] args) {DumbSet set = new DumbSet(); String s1 = “foo”; String s2 = “bar”;set.insert(s1); set.insert(s2);System.out.println(s1+”in set?”+set.member(s1)); String s = (String)set.any(); // downcast } System.out.println(“got “+s); }

CMCS 433, Fall 2002 - Michael Hicks 54

Wrapper classes

  • To create Integer , Boolean , Double , …
    • that is a subclass of Object
    • useful/required for polymorphic methods
      • HashTable, LinkedList, …
    • used in reflection classes
  • Include many utility functions
    • e.g., convert to/from String
  • Number : superclass of Byte , Short , Integer , Long , Float , Double - allows conversion to any other numeric primitive type

This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.