Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming Week 2 James King 12 August 2003 2 Creating Instances of Objects Basic Java Language Section.

Similar presentations


Presentation on theme: "1 Programming Week 2 James King 12 August 2003 2 Creating Instances of Objects Basic Java Language Section."— Presentation transcript:

1

2 1 Programming Week 2 James King 12 August 2003

3 2 Creating Instances of Objects Basic Java Language Section

4 3 Modelling multiple People/entities/things Often you need to model lots of monsters such as in a castle It would be inefficient to separately write code for each Monster you will use We can manufacture multiple instances of a class such as Monster easily The class is a template for manufacturing instances

5 4 Object oriented concepts Manufacturing Instances Monster snotty = new Monster(); Monster grotty = new Monster(); Note: Java is case sensitive. By convention instances of classes start with a lower case letter and classes with an upper case letter Name of class to create an instance of Name of object to store the instance The type of this instance (should be same as Name of class on the right of the new)

6 5 Object oriented concepts Instances An instance is an object of a particular class e.g grotty and snotty are the names for the instances of the Monster class we created Each instance has the same behavior Each instance has its own independent copy of the attributes When we create an instance of a class the attributes can be given starting values (about this more later)

7 6 In Java code we send messages to the names of the instances like this: snotty.take_damage(); grotty.take_unusual_damage(200); These messages will change the state of the object snotty by decreasing its health by 10 and grotty by decreasing its health by 200 – OUCH! Sending messages to an instance Calls snotty’s take damage with no parameters Calls grotty’s take unusual damage with a parameter value 200

8 7 How Parameters Work class Monster { int health; void take_unusual_damage(int damage) { health = health - damage; } snotty.take_unusual_damage(200); // More code When this line is executed control jumps to the take_unusual_damage method inside the snotty instance of Monster Damage is initialised to 200 I.e. the value of the parameter take_unusual_damage was called with Then the code inside the method is run When the method has been completed the next line after the call to the method is run

9 8 Initialising Instances during creation Basic Java Language Section

10 9 Initialising Instances When new is used to create an object the attributes are created. We can initialise its attributes. This is done by creating a method with the same name as the class This special method is called the constructor

11 10 Initialising Objects class Hero { int bullets;// attribute (state) Hero() // constructor called during new Hero(); { bullets = 10; } h = new Hero(); // more code Calls the constructor in Hero to initialise the instance before storing it in h

12 11 Method and Constructor Overloading Basic Java Language Section

13 12 Method Overloading It is possible to have two methods within the same class with the same names but different parameters This is sometimes useful for instance to provide default values The system can tell which you are using because of the type and number of parameters you pass

14 13 Method Overloading class Monster { int health;// attribute (state) void take_damage(int damage) // method with one parameter {health = health - damage; } void take_damage() // method with no parameters {health = health- 10; }

15 14 Method Overloading Now you can make a monster take a specified amount of damage snotty.take_damage(100); Or use the default damage rate of 10 snotty.take_damage(); Note its not just methods that can be overloaded this way constructors can be too

16 15 Constructor Overloading class Monster { int health Monster() {health = 10; } Monster(int initial_health) {health = initial_health; } grotty = new Monster(100); snotty = new Monster(); Creates an instance of a Monster with health 100 and stores it in grotty Creates another instance of a Monster health 10 and stores it in snotty

17 16 Inheritance Basic Java Language Section

18 17 Relationships between objects Sometimes the behaviour and attributes of one class are a superset of another For instance sciencefictionHero may be a superset of Hero with added behaviour such as laser_attack and extra attributes such as the number of batteries for the laser. It would be nice to be able to say sciencefictionHero is a kind of Hero but with specific additions to make it a sciencefictionHero

19 18 Inheritance, Subclasses and Superclasses We can inherit the attributes and behaviour (methods) from one class when we create another class SciencefictionHero is subclass of Hero – more specialised Hero is superclass of SciencefictionHero – more generalised The SciencefictionHero class can have additional behaviours and attributes

20 19 Inheritance class SciencefictionHero extends Hero { int laser_batteries; void laser_attack() { laser_batteries = laser_batteries -1; } Lecturer automatically has the attributes and methods Hero has…

21 20 Inheritance Since SciencefictionHero extends Hero SciencefictionHero can still use a gun SciencefictionHero h=new SciencefictionHero (); h.shoot(); h.laser_attack();

22 21 Inheritance Diagram We can illustrate the relationship between sciencefictionHero and Hero in a diagram Hero SciencefictionHero

23 22 Inheritance Overriding Sometimes the superclass has behaviour which is not quite what we want in our subclass. For instance it may be the case that we want a dragon will roar when it takes_damage.. But monster does not do this A subclass may have methods which override inherited methods of the same name in the superclass

24 23 Getting the old behaviour We can call the superclass to perform the overridden behaviour if we want using the word super to refer to the super class…

25 24 Object oriented concepts Inheritance class Dragon extends Monster { void take_damage() { roar(); super.take_damage(); } This overrides the take_damage method defined in Monster As part of the new behaviour take_damage calls the take_damage in Monster to reduce its health

26 25 Inheritance and Overriding Example snotty = new Monster() fang = new Dragon() snotty.take_damage() fang.take_damage() fang.take_damage(200) calls the take damage method in Monster calls the take damage method in Dragon calls the take damage method in Monster

27 26 Abstract to build frameworks Basic Java Language Section

28 27 Abstract methods and classes Sometimes it is useful to create a class which acts as a skeleton that someone else will subclass and fill in the details If a method in a class is abstract you may not create an instance of that class Any subclass should provide bodies for the abstract methods Abstract classes don’t have to have abstract methods and can contain everything a regular class can contain

29 28 Abstract Methods abstract class Victim { abstract public void take_damage(); } class Monster extends Victim { int health; public void take_damage() { health=health-10; } Monster must provide code for the take damage method it inherits from Victim

30 29 Constants Basic Java Language Section

31 30 Final Classes, Methods and Attributes Sometimes you don’t want someone to be able to subclass your class override a method in your class modify an attribute in your class Use final as part of its declaration

32 31 A final Class final class biro { public void draw() {// code } // no subclasses of this class can be declared

33 32 Final Methods class feather { public final void draw() { //code } // we can subclass feather but we cannot overload the draw method

34 33 Final Attributes Final as part of the declaration of an attribute stops it from being modified. class constants { public final float pie=3.14; public void crazy() { pie = 7.15; // illegal pie is declared final }


Download ppt "1 Programming Week 2 James King 12 August 2003 2 Creating Instances of Objects Basic Java Language Section."

Similar presentations


Ads by Google