CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)1CMSC433, Fall 2002Programming Language Technology and ParadigmsBasic JavaMichael HicksSep 10, 2002CMCS 433, Fall 2002 -Michael Hicks28Last Time•OO principles–Keys: abstraction, encapsulation, sharing•Java basics–Everything is an Object•Object “contract”•Downcasting–Objects are always referenced on the HeapCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)2CMCS 433, Fall 2002 -Michael Hicks29Visibility Modifiers•Indicate visibility of– Classes–Methods–Fields•Support abstraction–Clients unaffected by change in implementation•Support encapsulation–Prevents leaking of information to clientsCMCS 433, Fall 2002 -Michael Hicks30Class 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 scopeCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)3CMCS 433, Fall 2002 -Michael Hicks31Variable / method visibility•public–visible everywhere•private–visible only within this class•protected – visible within same package or in subclass•package(default) –visible within same packageCMCS 433, Fall 2002 -Michael Hicks32Instance vs. static variables• static –the data is stored “with the class”–static variables allocated once, no matter how many objects created–static methods are not specific to any class instance, so can’t refer to thisor super• Can reference class variables and methods through either class name or an object ref–Clearer to reference via the class nameCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)4CMCS 433, Fall 2002 -Michael Hicks33Instance vs. staticint fooint fooint fooFoointbar;Public class Foo {int foo;static int bar;}Class definitionClass implementationObjects of class FooCMCS 433, Fall 2002 -Michael Hicks34Examples•public staticvoid main(Stringargs[]) { … }•public class Math {public final staticPI = 3.14159…;}•public class System {public staticPrintStreamout = …;}CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)5CMCS 433, Fall 2002 -Michael Hicks35Instance variable modifiers•final–can’t be changed; must be initialized in declaration or in constructor•transient, volatile–will cover laterCMCS 433, Fall 2002 -Michael Hicks36Method 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 laterCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)6CMCS 433, Fall 2002 -Michael Hicks37Method invocation•Method names can be overloaded–method invoked is determined by both its name and the types of the parameters–resolved at compile-time, based on compile-time types•Methods can also be overridden–define a method also defined by a superclass–arguments and result types must be identical–resolved at run-time, based on type of object method is invoked onCMCS 433, Fall 2002 -Michael Hicks38Overriding•Overriding–methods with same name and argument types in child class override method in parent class–you can override/hide instance variables•both variables will exist, but don’t do itclass Parent {int cost;void add(int x) {cost += x;}}class Child extends Parent {void add(int x) {if (x > 0) cost += x;}}CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)7CMCS 433, Fall 2002 -Michael Hicks39Overloading•Methods with the same name, but different parameters (count or types) are overloadedclass Parent {int cost;void add (int x) {cost += x;}void add (String s) throws NumberFormatException{cost += Integer.parseInt(s);}}CMCS 433, Fall 2002 -Michael Hicks40Dynamic Method Dispatch•If you have a ref aof type Ato an object that is actually of type B(a subclass of A)–instance methods invoked on awill get the methods for class B(like C++ virtual functions)–class methods invoked on awill get the methods for class A•invoking class methods on objects strongly discouragedCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)8CMCS 433, Fall 2002 -Michael Hicks41Simple Dynamic Dispatch Examplepublic 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 Hicks42Self reference•this refers to the object the method is invoked on•superrefers to the same object as this–but used to access methods/variables in superclass•Like C++CMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)9CMCS 433, Fall 2002 -Michael Hicks43Constructors•Declaration syntax same as C++–no return type specified–method name same as class•First statement can be this(args)or super(args)–if those are omitted, super()is called–must be very first statement, even before variable declarations•notused for type conversions or assignments•void constructor generated if no constructors givenCMCS 433, Fall 2002 -Michael Hicks44Garbage collection•Objects that are no longer accessible can be garbage collected•Method void finalize()called when an object is collected–best to avoid using it, since no way to tell when it will get called• Garbage collection not a major performance bottleneck–new/delete in C++ can be expensive tooCMSC 433,Michael Hicks, U. Maryland (via W. Pugh, A. Sussman, and A, Porter)10CMCS 433, Fall 2002 -Michael Hicks45Detailed Example•Shows–polymorphism for both method receiver and arguments–static vs. instance methods–overriding instance variablesCMCS 433, Fall 2002 -Michael Hicks46Source code for classesclass 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
View Full Document