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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
Object Oriented Software Development
1 Programming James King 12 August Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple.
Inheritance using Java
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Programming Week 2 2 Inheritance Basic Java Language Section.
Programming in Java CSCI-2220 Object Oriented Programming.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
1 Programming 2 Aims Give overview of concepts addressed in Web based programming module Teach you enough Java to write simple web and network applications.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Class Inheritance Part I
Final and Abstract Classes
An Introduction to Inheritance
Object Oriented Programming
Can perform actions and provide communication
Can perform actions and provide communication
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Java – Inheritance.
Can perform actions and provide communication
Java Inheritance.
Inheritance and Polymorphism
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Java Programming Language
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Presentation transcript:

1 Programming Week 2 James King 12 August 2003

2 Creating Instances of Objects Basic Java Language Section

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

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)

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)

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

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

8 Initialising Instances during creation Basic Java Language Section

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

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

11 Method and Constructor Overloading Basic Java Language Section

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

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; }

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

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

16 Inheritance Basic Java Language Section

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

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

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…

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

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

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

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…

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

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

26 Abstract to build frameworks Basic Java Language Section

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

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

29 Constants Basic Java Language Section

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

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

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

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 }