[email protected] applications development objects in java key concepts and metaphors...

11
[email protected] Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance variables Dangers of uninitialised object references

Upload: georgiana-evans

Post on 27-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentObjects in Java

Key concepts and metaphorsImplicit subclassingStatic methodsConstructorsInstance variablesDangers of uninitialised object references

Page 2: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentMetaphors - inheritance

A small UK company (Watsonian Squire Ltd) imports “Royal Enfield” motorcycles from India where they are still in production

The basic design has not changed substantially since 1949!

The UK company does two things:checks the imported bikes and sells them on

directlyor modifies the bikes into “Clubman” and “Trailie”

styles by changing some components

Page 3: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentMetaphors - inheritance

The company obtains an instance of a “basic motorcycle” superclass object and can use it directly…

or it can subclass the object and “override” a feature by providing a new or modified part or function

This would not be possible without the cooperation of the Indian factory. The style of the motorcycle is not final.

Page 4: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentWhat about a simple object?

To subclass we do this:public class Spoon extends Cutlery { .. }

But what does this do?public class TaxCalcLib { … }

Any class definition with no explicit extends clause is inheriting from java.lang.Object

This provides the bare minimum framework for a class to function

Page 5: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

Structure of a simple classimport java.net.*;public class HostName{ public static void main(String[] args)

{ InetAddress iaLocal;try{ iaLocal = InetAddress.getLocalHost();

System.out.println(“Name:" + iaLocal.getHostName());

}catch (UnknownHostException e){ System.err.println("Don't know about local host");

System.exit(0);}

}} // end of class HostName

Page 6: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentStatic methods

Note that the example program is part of an object called “HostName”

Thus the filename must be HostName.javaBut how does the object get instantiated?Surprise - it doesn’t!The example uses a static method (code

that’s part of the class, not the object)

Page 7: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

Object instantiationpublic class Egg{ private String myColour;

public Egg(String sEggColour) // NB no return type!{ myColour = sEggColour;}public Egg() // NB no return type!{ myColour = “Orange”;}public static void main(String[] args){ Egg e1 = new Egg(“Green”); // create some objects…

Egg e2 = new Egg(); // what colour is this?Egg e[] = new Egg[6]; // no objects created

here!…

}} // end of class Egg

Page 8: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentConstructors

A “constructor” is an initialisation methodIt runs when a new object is createdA class may define many object constructorsEach constructor differs only in its input

parametersAll methods can have multiple “flavours”However, unlike other methods, constructors

have no return type

Page 9: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

public class Egg{ private String myColour; // each instance gets its own copy

public Egg(String sEggColour){ myColour = sEggColour;}public Egg(){ myColour = "Orange";}public void reportColour(){ System.out.println(myColour);}public static void main(String[] args) // static – all eggs share this{ Egg e1 = new Egg("Green"); // green egg

Egg e2 = new Egg(); // orange eggEgg e[] = new Egg[6]; // six references to eggs// …e1.reportColour(); // should be greene2.reportColour(); // should be orangee[0].reportColour(); // runtime error – no eggs here yet

}} // end of class Egg

Page 10: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentComment on the examples

The idea of using a static method of a class to instantiate objects of the same type is common practice (e.g. in small programs, for testing)

It seems counter-intuitive to some peopleA clear separation between the concepts of

static, class-based code anddynamic, object-based code

…helps us gain a better perspective

Page 11: N.A.Shulver@staffs.ac.uk Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance

[email protected]

Applications DevelopmentConclusion

We have seen:some of the concepts and language

associated with object-orientationsome practical issues of program structure,

including the use of static methodsconstructors and their use