Introduction To Programming Using JAVA - Lecture 5
This document provides an introduction to programming using Java. It defines objects and classes, explaining that objects have attributes and behaviors while classes are templates for objects. It gives examples of an Employee object and class. It also covers creating objects using the new operator, accessing object methods, constructors for initializing objects, and the this and super keywords. The summary provides key definitions and concepts in the document for understanding objects and classes in Java.
Introduction To Programming Using JAVA - Lecture 5
This document provides an introduction to programming using Java. It defines objects and classes, explaining that objects have attributes and behaviors while classes are templates for objects. It gives examples of an Employee object and class. It also covers creating objects using the new operator, accessing object methods, constructors for initializing objects, and the this and super keywords. The summary provides key definitions and concepts in the document for understanding objects and classes in Java.
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved.
No part of this material may be reproduced and sold.
1 Introduction to Programming Introduction to Programming ( ( Using Java) Using Java) Nandika Kasun Nandika Kasun University of Colombo School of Computing (UCSC) University of Colombo School of Computing (UCSC) University of Colombo University of Colombo Sri Lanka Sri Lanka Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 2 Definition of a(n) Object/Class Definition of a(n) Object/Class What is an Object : A thing may have a physical presence such as a table ,chair or an abstract concept such as a job . An object is an abstract representation of a thing in the real world. We simulate a problem domain in the real- world through objects. Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 3 Objects Objects An object has a unique identity, attributes (What it knows or data about it), and behavior (What it can do). Eg Eg : : An Employee object (say employee) will have the following attributes (what it knows): name, age, salary It will have the following behavior (what it can do): set salary, get salary, set name, set age Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 4 Classes Classes What is a Class? Objects are the representation of things in the real world Classes are templates for objects, ie. They contain all the common properties and behavior of a group of objects. All objects with the same characteristics and behaviors belongs to the same class. Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 5 Java Application Java Application A Java application is a collection of one or more Java classes. At least one of the classes is public and contains a method named main() except in case of applets.. Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 6 public class Employee { private String name; private int age; private float salary; public void setName(String tName) { Name = tName; } public void setSalary (float tSalary) { salary = tSalary; } public float getSalary () { return salary; } } Objects and Classes Objects and Classes An example class Declaration Attributes Methods Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 7 Creating Objects Creating Objects Objects are created by instantiating classes. To use a class in a program, you must first create an instance of it. Objects of a class can be created using the new new operator. Example Employee newEmp = new Employee(); Class Name Variable containing an Object reference Constructor Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 8 Creating Objects Creating Objects Object References following declaration will create an Object reference Employee newEmp2 ; The newEmp2 variable will have value null newEmp2 You can create multiple References to the same object Employee newEmp; newEmp = new Employee(); newEmp2 = newEmp; newemp newemp2 newEmp Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 9 Destroying Objects Destroying Objects Javas garbage collection is automatic There is no great need for an explicit destructor. finalize method plays the role of a destructor. This method is useful to disconnect network connections as well as close any open databases Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 10 Objects and Classes Objects and Classes A basic method definition is as follows: modifier returnType MethodName (parameter List) { statement(s); } Optional Can be any primitive type or a class Name or void (no return statement) Usually the modifier for methods is public and for attributes it is private Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 11 Objects and Classes Objects and Classes The return Type, Method Name, and the parameter list defines the Signature of the method It is possible to define two or more methods with the same name within the same class (Method Overloading) with different signatures public void CreatePoint( ) public void CreatePoint(int x , y) Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 12 Objects and Classes Objects and Classes Accessing methods a instance method can be accessed using the dot(.) operator as shown below. ObjectName.method(); A class method can be accessed by using the class name followed by a period and then the method name Integer.parseInt(25); Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 13 Passing parameters all parameters (except Object types such as instances of classes and Arrays etc.) are passed by value. i.e. any modifications done within the method does not affect the original variable. Objects and Classes Objects and Classes if you require to modify the original variables, and needs to pass those to a method Declare those as instance variables in a class and pass an object of that class to the method Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 14 Constructors Constructors Constructor Methods These methods are used to initialize objects they have the same name as the class and have no return type these methods are called automatically when the new operator is used to allocate memory for an object. Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 15 Constructors Constructors Overloading Constructors A class can have multiple Constructors (Overloaded Constructors) All carries the same name They have either different number of arguments or different types of arguments Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 16 Constructors Constructors Example for Constructors public class twoDPoint { float x,y; twoDPoint ( ) { x= y = 0; } } No argument Constructor Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 17 Constructors Constructors Example for Constructors public class twoDPoint { float x,y; twoDPoint ( ) { x= y = 0; } twoDPoint ( float a , float b) { x=a; y=b; } } No argument Constructor Overloaded constructor Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 18 This Keyword This Keyword this keyword this keyword is used to refer to the current object. It can be used to : refer to the current objects member variables refer to current objects methods pass a reference to the current object to a method return a reference current object Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 19 This Keyword This Keyword this keyword t=this.x; this.myMethod(this); return this; Refers to the member variable x in this object Call the myMethod defined in this class and pass its this Object Return this Object Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 20 Super Keyword Super Keyword super keyword This keyword is used to refer to the Super or parent class super( ); super(x,y); super.f( ); Invokes super class Constructor Invokes super class Constructor with x and y arguments Calls the super class function called f( ) Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2003. All rights reserved. No part of this material may be reproduced and sold. 21 Summary Summary This week we learned Objects and Classes Exercise: Exercise: Implement Java objects for represent a Chair, a Car and a Implement Java objects for represent a Chair, a Car and a Student. You should define attributes and methods of these Student. You should define attributes and methods of these objects. objects.