phy281 scientific java programming objectsslide 1 classes & objects in this section we will...

16
PHY281 Scientifi c Java Programmi ng Objects Slide 1 Classes & Objects In this section we will learn about Classes and Objects in Java : What are Objects? Creating a Class Class Variables Methods Using Classes Inheritance Constructor Methods Overloading Methods

Upload: rosalyn-thornton

Post on 20-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 1

Classes & Objects

In this section we will learn about Classes and Objects in Java :

What are Objects?

Creating a Class

Class Variables

Methods

Using Classes

Inheritance

Constructor Methods

Overloading Methods

Page 2: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 2

What are Objects?

In Object-Oriented Programming Languages - such as Java and C++ in addition to simple variables like int - you can work with more complex 'Objects'.Just like the real world - everything can be thought of as an Object - perhaps made up of smaller Objects.

A person is an Object and consists of arms, legs etc which are also Objects. Arms consists of fingers, elbows etc and so on.

Similarly like the real world - Objects have two things:

• Attributes - how it is described, what properties it has - its member variables.

• Behaviour - what it can do - the functions or methods you can call.

Page 3: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 3

Creating a Class

You create an object from a template known as a class. A simple program may only have one class whereas more complex ones may have many.

public class Dog { String name; public void speak(Graphics g, int x, int y) { g.drawString("Woof: my name is " + name, x, y); }}

So it can be used by others The name of the Class

An attribute

A method

Page 4: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 4

Class Variables

The class just defines the object. To create an actual instance of a an object you have to use the new statement.

Dog dog1 = new Dog();

The name of the Class

The name of this instance of the class

When you create an object with new, the object gets its own version of any variables object variables which are independent of any other objects of the same class.

However if you use static then the variable is shared amongst all the objects of that class and called a class variable.

public class Dog { String name; static int numberofDogs;}

Each Dog object has its own name variable

All Dog object access the same variable numberofDogs

Page 5: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 5

Methods

You use methods to allow you classes to do things. A method is declared in a similar way to a class with a number of arguments in brackets ( ) but unlike a class a method can return a value (a simple value such as an integer or an object). If no return value is required then void should be used.

public class MyCounter { int sum;

public void addtoSum(int amount) { sum += amount; }

public int getSum() { return sum; } public void printSum(Graphics g, int x, int y) { g.drawString("The sum is " + sum, x, y); }}

Page 6: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 6

Using a Class

class Animals { public static void main (String[] arguments) { Dog dog1 = new Dog(); dog1.name = "Fido"; Dog dog2 = new Dog(); dog2.name = "Butch"; dog1.speak(); dog2.speak(); }}

public class Dogs extends Applet {

Dog dog1; Dog dog2;

public void init() { dog1 = new Dog(); dog1.name = "Fido"; dog2 = new Dog(); dog2.name = "Butch"; }

public void paint(Graphics g) { dog1.speak(g, 10, 20); dog2.speak(g, 10, 40); }}

class Dog { public String name; public void speak(Graphics g, int x, int y) { g.drawString("Woof: my name is " + name, x, y); }}

Create a new instance of the class called dog1

Set the attribute name

Call the method speak

Page 7: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 7

Inheritance

One of the most powerful features of Object-Oriented programming is called Inheritance. Inheritance means that an object can inherit attributes and behaviour from other (similar) objects. This way you do not have to make a complete copy if you want to make a new class with slightly different features.

Mammal

Dog Cat

Poodle Spaniel

Page 8: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 8

Building Inheritance

Uses identify from the Mammal class

class Mammal { public String name; public void identify(Graphics g, int x, int y) { g.drawString("my name is " + name, x + 50, y); }}

class Dog extends Mammal { public void speak(Graphics g, int x, int y) { g.drawString("Woof!", x, y); identify(g, x, y); }}

class Cat extends Mammal { public void speak(Graphics g, int x, int y) { g.drawString("Mieow!", x, y); identify(g, x, y); }}

Use extends to inherit from the Mammal class

Page 9: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 9

Building Inheritance

public class Animals extends Applet {

Dog dog1; Dog dog2; Cat cat1;

public void init() { dog1 = new Dog(); dog1.name = "Fido";

dog2 = new Dog(); dog2.name = "Butch";

cat1 = new Cat(); cat1.name = "Tiger"; }

public void paint(Graphics g) { dog1.speak(g, 10, 20); dog2.speak(g, 10, 40); cat1.speak(g, 10, 60); }}class Mammal ...class Dog ...class Cat ...

Page 10: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 10

Constructor Methods

When you create a new object of a class (called an instance of the class) using the new statement a special method called a constructor is called. You can use the constructor to set up variables etc. The constructor is like any other method but does not return a value. If you do not supply a constructor then a default one is used which has no arguments and does nothing.

public MyClass() {}

The default constructor for a class MyClass:

A more useful constructor for the class MyClass:public MyClass(String name) { myName = name; // Initialize myName}

Page 11: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 11

Constructors

class Dog extends Mammal { public Dog(String myname) { name = myname; } public void speak(Graphics g, int x, int y) { g.drawString("Woof!", x, y); identify(g, x, y); }}

class Cat extends Mammal { public Cat(String myname) { name = myname; } public void speak(Graphics g, int x, int y) { g.drawString("Mieow!", x, y); identify(g, x, y); }}

You would like to put the constructor in the base class Mammal but unfortunately constructors aren't inherited.

Page 12: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 12

Constructors

Now you can give the animal a name when you create it instead of having to do it later.

public class Animals extends Applet {

Dog dog1; Dog dog2; Cat cat1;

public void init() { dog1 = new Dog("Fido"); dog2 = new Dog("Butch"); cat1 = new Cat("Tiger"); }

public void paint(Graphics g) { dog1.speak(g, 10, 20); dog2.speak(g, 10, 40); cat1.speak(g, 10, 60); }}class Mammal ...class Dog ...class Cat ...

Page 13: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 13

Overloading Methods

You can have methods with the same name but with different arguments - this is known as overloading. Often this is used to provide default values for some of the arguments.

class Dog extends Mammal { public Dog (String myname) { name = myname; } public Dog () { name = "Unknown"; } public void speak(Graphics g, int x, int y) { g.drawString("Woof!", x, y); identify(g, x, y); }}

Same method name but different arguments

Page 14: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 14

Overloading Methods

public class Animals extends Applet {

Dog dog1; Dog dog2; Dog dog3; Cat cat1;

public void init() { dog1 = new Dog("Fido"); dog2 = new Dog("Butch"); cat1 = new Cat("Tiger"); dog3 = new Dog(); }

public void paint(Graphics g) { dog1.speak(g, 10, 20); dog2.speak(g, 10, 40); cat1.speak(g, 10, 60); dog3.speak(g, 10, 80); }}class Mammal ...class Dog ...class Cat ...

Page 15: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 15

Using a Class Variable

class Dog extends Mammal { static int total;

public Dog (String myname) { name = myname; total++; } public Dog () { name = "Unknown"; total++; } public void speak(Graphics g, int x, int y) { g.drawString("Woof!", x, y); identify(g, x, y); } public void count(Graphics g, int x, int y) { g.drawString("There are " + total + " dogs", x, y); }}

Add a class variable to count the number of dogs and a new method to print it out:

Page 16: PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?

PHY281 Scientific Java Programming

Objects Slide 16

Using a Class Variable

public class Animals extends Applet {

Dog dog1; Dog dog2; Dog dog3; Cat cat1;

public void init() { dog1 = new Dog("Fido"); dog2 = new Dog("Butch"); cat1 = new Cat("Tiger"); dog3 = new Dog(); }

public void paint(Graphics g) { dog1.speak(g, 10, 20); dog2.speak(g, 10, 40); cat1.speak(g, 10, 60); dog3.speak(g, 10, 80); dog1.count(g, 10, 100); dog3.count(g, 10, 120); }}

It doesn't matter which object you use to call count( ) as they both access the same total variable