Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?"— Presentation transcript:

1 PHY281 Scientific Java Programming ObjectsSlide 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

2 PHY281 Scientific Java Programming ObjectsSlide 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.

3 PHY281 Scientific Java Programming ObjectsSlide 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 othersThe name of the ClassAn attribute A method

4 PHY281 Scientific Java Programming ObjectsSlide 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

5 PHY281 Scientific Java Programming ObjectsSlide 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); }

6 PHY281 Scientific Java Programming ObjectsSlide 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

7 PHY281 Scientific Java Programming ObjectsSlide 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 DogCat PoodleSpaniel

8 PHY281 Scientific Java Programming ObjectsSlide 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

9 PHY281 Scientific Java Programming ObjectsSlide 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...

10 PHY281 Scientific Java Programming ObjectsSlide 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 }

11 PHY281 Scientific Java Programming ObjectsSlide 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.

12 PHY281 Scientific Java Programming ObjectsSlide 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...

13 PHY281 Scientific Java Programming ObjectsSlide 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

14 PHY281 Scientific Java Programming ObjectsSlide 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...

15 PHY281 Scientific Java Programming ObjectsSlide 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:

16 PHY281 Scientific Java Programming ObjectsSlide 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


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

Similar presentations


Ads by Google