Introduction Refresher - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Introduction Refresher

Description:

... e1 = new Egg('Green'); // create some objects... Egg e2 = new Egg(); // what ... { Egg e1 = new Egg('Green'); // green egg. Egg e2 = new Egg(); // orange egg ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 13
Provided by: Staf196
Category:

less

Transcript and Presenter's Notes

Title: Introduction Refresher


1
Introduction / Refresher
  • We will be using Java Server Pages (JSP) in this
    module
  • However, this introductory refresher looks at
    fundamental OO principles
  • This lecture uses basic Java notation
  • We talk about inheritance
  • And classes in Java
  • Mention polymorphism and encapsulation

2
Metaphors - 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
    directly
  • or modifies the bikes into Clubman and
    Trailie styles by changing some components

3
Metaphors - 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.

4
What 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

5
Structure of a simple class
  • import 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

6
Static methods
  • Note that the example program is part of an
    object called HostName
  • Thus the filename must be HostName.java
  • But how does the object get instantiated?
  • Surprise - it doesnt!
  • The example uses a static method (code thats
    part of the class, not the object)

7
Object instantiation
  • public 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 Egg6 // no objects created
    here!
  • // end of class Egg

8
Constructors
  • A constructor is an initialisation method
  • It runs when a new object is created
  • A class may define many object constructors
  • Each constructor differs only in its input
    parameters
  • All methods can have multiple flavours
  • However, unlike other methods, constructors have
    no return type

9
  • 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 egg
  • Egg e new Egg6 // six references to
    eggs
  • //
  • e1.reportColour() // should be green
  • e2.reportColour() // should be orange
  • e0.reportColour() // runtime error no eggs
    here yet

10
Comment 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 people
  • A clear separation between the concepts of
  • static, class-based code and
  • dynamic, object-based code
  • helps us gain a better perspective

11
Class types - review
  • Classes (the blueprints) come in various types
  • abstract class part-baked class, cannot be
    instantiated, must be extended extends
  • interface class capability declaration only,
    no code content, must be implemented implements
  • final class may be instantiated but not
    extended
  • class may be instantiated or extended

12
Polymorphism and Encapsulation
  • Poly many, morph form
  • Different classes derived from the same base
    class can be polymorphic
  • Example Abstract Shape class, derived Square and
    Circle classes
  • Encapsulation is good practice keep your
    private methods and data hidden inside your
    objects
  • Encapsulation protects against the
    cascade-of-changes problem in 3GLs
Write a Comment
User Comments (0)
About PowerShow.com