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

Slides:



Advertisements
Similar presentations
Phil Campbell London South Bank University Java 1 First Steps.
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
More about Classes and Objects. Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
ECE122 Feb. 22, Any question on Vehicle sample code?
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Classes and Objects in Java
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Inheritance Fundamentals. Inheritance A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
93 4/11/98 CSE 143 Class Constructors [Sections ]
Classes - Intermediate
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Learners Support Publications Constructors and Destructors.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Objects as a programming concept
More About Objects and Methods
March 29th Odds & Ends CS 239.
CMSC 202 Interfaces.
Polymorphism 28-Nov-18.
Classes & Objects: Examples
Constructors and Destructors
CS2011 Introduction to Programming I Objects and Classes
Section 9.1 Introduction.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
CMSC 202 Interfaces.
Polymorphism 15-Apr-19.
Java Programming Language
Polymorphism 21-Apr-19.
CMSC 202 Interfaces.
Object-Oriented Programming and class Design
C++ Object Oriented 1.
Presentation transcript:

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

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.

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

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

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

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

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

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

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

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 }

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.

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

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

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

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:

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