Video

Thursday 30 June 2011

lesson 5-Introduction to Classes

5. Introduction to

Classes

Objects & Classes

Import a class

BankAccount Class

Constructor

Copying Object

Reference

Objects & Classes

C is a structured

programming language

and its structures are

such as structs, unions,

and procedures.

Java, on the other

hand, consists of

methods, interfaces,

and a much more

highly developed class

structure.

The class keyword

introduces a class

definition and is

followed by the class

name.

The nameof the class

is called identifier.

Objects & Classes

Object is like a black

box which has calling

methods for doing

something

Object or black box

has part of code and

data

System.out is the

object of (Class)

PrintStream

Therefore, Class is like a

object's factory.

Class will define the details

of object's data and code

for object's method

Java Syntax for Object

Construction

new ClassName

(parameters)

When object is built by

syntax "new" it will

return the reference of

object

Example of Class :

Shape

Import a class

import

packageName.ClassName;

import java.awt.Rectangle;

// Class Rectangle is in

package named

java.awt

// (awt abbreviate

from Abstract

Windowing Toolkit)

For String and System

class are in package

named java.lang

which they do not

need to import

because it is import

automatically

We can import every

class in any package

by

import packagename.*;

import java.awt.*;

import java.io.*;

becareful!

import java.*.*; //

wrong

If we not import at the

start of program, we

should do like this:

java.awt.Rectangle

rect1;

rect1 = new

java.awt.Rectangle(10,

15, 20, 30);

Example

// Comparison.java

import javax.swing.

JOptionPane;

public class

Comparison {

public static void main

(String args[])

{

String firstnumber,

secondnumber.

int num1, num2;

firstnumber =

JOptionPane.

showInputDialog

(“Enger first integer”);

secondnumber =

JOptionPane.

showInputDialog

(“Enger second

integer”);

num1 =

Integer.parseInt

(firstnumber);

BankAccount Class

public class

BankAccount

{ private double

balance; // instance

variables

public void deposit

(double amount) //

method

{ balance = balance +

amount; }

public void withdraw

(double amount)

{balance = balance -

amount; }

public double

getBalance()

{ return balance; }

}

Constructors

A constructor is used

to initialize the

instance variables of

an object.

They have the same

name as their class.

They are always

invoked with the new

operator.

The new operator

allocates memory for

the object.

A default constructor

takes no parameters.

A general constructor

can take parameters.

Constructors

public class BankAccount

{ private double

balance;

public BankAccount() //

Default Constructors

{ balance = 0; }

public BankAccount

(double init) //

Constructors

{ balance = init; }

public void deposit

(double amount)

{ balance = balance +

amount; }

public void withdraw

(double amount)

{balance = balance -

amount; }

public double

getBalance()

{ return balance; }

}

How to use

BankAccount class

Once, we create a

class, we can create a

new object of this

class using new

command.

For example,

BankAccount myChecking

= new BankAccount();

Since we declare this

class as public, we can

call this class from

other classes.

How to use

BankAccount class

public class

BankAccountTest

{ public static void

main(String[] args)

{ BankAccount account

= new BankAccount

(10000);

final double INT_RATE

= 5;

double interest;

// compute and add

interest

interest =

account.getBalance() *

INT_RATE / 100;

account.deposit

(interest);

System.out.println

(“Balance after year 1

is”

+ account.getBalance()

);

// add interest again

interest =

account.getBalance() *

INT_RATE / 100;

account.deposit

(interest);

}

}

Copying Numbers

double balance1 =

1000;

double balance2 =

balance1; // figure (a)

balance2 = balance2 +

500; // figure (b)

Copying Object

References

BankAccount account1

= new BankAccount

(1000);

BankAccount account2

= account1;

account2.deposit(500);

null Reference

BankAccount account1

= new BankAccount

(1000);

BankAccount account2

= account1;

account1 = null; // an

object variable that

refers to no object.

String greeting =

“Hello”;

String message = “”; //

an empty string

String comment =

null; // refers to no

string at all

int g = greeting.length

(); // return 5

int m =

message.length(); //

return 0

int c = comment.length

(); // program

terminate

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Sweet Tomatoes Printable Coupons