Professional Documents
Culture Documents
private String name; private String model; private String color; private int year;
Objectives
At the end of this unit students should be able to: PT1
1. 2. 3. 4. 5. 6.
Outline the basic ideas behind object oriented programming State what is a class and how a class is related to an object Demonstrate the creation(definition) of a simple class structure (with variables and methods)
Define and use instance variables, class variables, instance methods and class methods
Define constructors for classes. Create objects of a class
PT2
7. 8. 9.
Describe method and constructor overloading and construct classes that have overloaded methods and constructors
Use the Java Class Library and existing classes Define, create and use java packages
10. Differentiate between the various access modifiers and when it is appropriate to
use them
cooperating objects, exchanging messages, for the purpose of achieving a common objective.
Objective:2
State what is a class and how a class related to an
object
Object
An object is a self-contained entity.
(ie. data or properties) and operations/methods (ie. behaviour ) that encapsulate functionality into a reusable and dynamically loaded structure.
Classes
The basic element of object-oriented programming
is a class.
A class: defines attributes /state which are variables which can store a value that represents a particular property defines the methods that manipulate the object or perform interaction between related objects and methods
Objective: #3
Demonstrate the creation(definition) of a simple
Methods
Classes contd.
A Class defines a new Reference Data Type
object and is a template for multiple objects with similar features. Once defined this new data type can be used to create objects of that type Thus, a class is a template for an object, and an object is an instance of a class. Example : Car audi = new Car();
Class contd.
When you declare a class, you declare its exact form
and nature. You do this by specifying the data that it contains(fields/attributes) and the code that operates on that data(methods). While very simple classes may contain only code or only data, most real world classes contains both
Classes contd.
The requirements for the
creation of a class include a source file with the <modifier>, class keyword, followed by a legal identifier <class_name> and a pair of curly braces for the body.
Syntax is: <modifier> class <class name>{ //<body of the class> <attribute_declaration>* <constructor_declaration>* <method_declaration>*
Classes contd.
Code:
public class Car{ //<body of the class> private String model; private String make; private int year; private String color; public Car(){ }
public void setMake(){ }
Objective:#4
Define and use instance variables, class variables,
class are contained within the method Collectively the methods and variables within the class are called members of the class.
Declaring Attributes
The elements of attribute declaration are:
<modifier> <type> <name>
Syntax is:
<modifier> <type> <name>
name, number of pages of a Book class are its attributes. So the definition of the Book class would be:
public class Book{ private String name; private String authorname; private int nopages;
Methods
Class behaviour are represented in Java by methods
in other programming languages Methods are a series of statements that perform some repeated task. Instead of writing 10 lines of
code we can put those ten lines in a method and just call it in one line.
Methods
Modules in Java are called methods and classes.
methods and classes the programmers write with prepackaged methods and classes available in the Java API Classes generally consists of two things:
Instance variables (Attributes)
Declaring Methods
The elements of method declaration are:
<modifier>: <return_type>: <method_name>: <parameters>:
Syntax is:
<modifier> <return_type> <method_name>(<parameters>){ //body of code }
Methods
return_type specifies the type of data returned
by the method. (this can be any valid type including class types that you create) If the method does not return a value, the type must be set to void
public String getName(){ return name; } public void setName(String name){ this.name=name; }
void, return a value to the calling routine using the following return statements:
Syntax is:
return value;
method.(this can be any legal identifier other than those already used by other items within the
current scope).
public String getName(){ return name; } public void setName(String name){ this.name=name; }
pairs separated by commas. Parameters are essential variables that receive the value of arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty.
public String getName(){ return name; } public void setName(String name){ this.name=name; }
local variables
All variable declared in method definitions they are known only in the method in which they are
defined.
Book - name: String - author: String - nopages: int + Book () + getName(): String + getAuthor(): String + getNoPages():int + setName(String):void + setAuthor(String):void +setNoPages(int):void
Objective:#5
Define constructors for classes.
Constructor
A constructor initializes an
object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, it is automatically called immediately after the object is created, before the new operator completes.
Book - name: String - author: String - nopages: int + Book () + getName(): String + getAuthor(): String + getNoPages():int + setName(String):void + setAuthor(String):void +setNoPages(int):void
Constructor
Constructors do not have a return type, not even
void It is the job of the constructor to initialize the internal state of an object, so that the code creating an instance will have fully initialized usable objects immediately Constructors like methods can receive parameters
//Constructor1 public Book(){ name = "Java Programming"; authorname=" "; nopages = 0; } //Constructor2 public Book(String n, String an, String n, int p){ name= n; authorname=an; nopages = p ; }
Constructors need not be explicitly called because constructors are called during the object creation and should be provided with the required parameters if any. Book java=new book(Patrick Norton); The above object creation(java) gives a call to the constructor which takes string parameter. Whenever such definition of the constructor or list of constructor are made it is said that the constructors are overloaded. Will be discussed in details later Such cases one constructor among the list of constructors is called depending upon the object creation
Objective:6#
Create objects of a class
Declaring Objects
When you create a class you are creating a new
data type. You can use this type to declare objects of that type.
Declaring Objects
Obtaining objects is a two step process.
1. 2.
Declare the variable of the class type (does not define and object Acquire a physical copy of the object and assign it to that variable. (this is done using the new operator.
Example:
Book b = new Book();
returns a reference to that object. The reference is then stored in the variable.
Example:
Book b= new Book();
b is the reference to an instance of Book.
when it is no longer referred to by any variable. When a new object is created a distinct set of instance variables are obtained. The instances of the class are created during the program execution and discarded as and when required.
create objects A class creates a logical framework that defines the relationship between members A class is like a blueprint from which an object is built.
Dot Operator
The dot notation is used to obtain the value of the
instance variables. <objectReference>.<variable name> Where objectReference is the name of the object and variable name is the instance variable.
Invoking a Method
A method is invoked (i.e. made to perform its
designated task)by a method call. The method call specifies the method name and provides information (as arguments) that the called methods needs to do its tasks
Method Call
Calling a method is similar to calling or referring to
an instance variable. The methods are accessed using dot operator along with the object.
Syntax is:
Objectname.Methodname(param1,param2,);
Example:
Book b = new Book(); b.setName(The World Prose);
Additional Reading
http://java.sun.com/docs/books/tutorial/java/package/namingpk