simple class and object examples in java

78
Simple class and object examples in Java Presented By Harish Gyanani

Upload: harish-gyanani

Post on 14-Jan-2015

830 views

Category:

Education


3 download

DESCRIPTION

Simple class and object examples in java classes objects fields properties variables instance variables local variables class variables methods members of class data members UML representation of class instance methods static methods create object instantiate object object qualifier reference to object allocate memory to object class naming conventions fields naming conventions

TRANSCRIPT

Page 1: Simple class and object examples in java

Simple class and object examples in Java

Presented By Harish Gyanani

Page 2: Simple class and object examples in java

What is class?

A class is a blue print from which individual objects are created.

Page 3: Simple class and object examples in java

Class naming convention

• By convention, class names capitalize the initial of each word.

• For example: Employee, Boss, DateUtility, PostOffice, RegularRateCalculator.

Page 4: Simple class and object examples in java

What are members of Class?

• Field: – field is nothing but the property of the class or object which

we are going to create .– Example if we are creating a class called computer then they

have property like model, memSize, hdSize, osType etc.

• Method:– method is nothing but the operation that an object can

perform it define the behavior of object how an object can interact with outside world.

– Example startMethod (), shutdownMethod ().

Page 5: Simple class and object examples in java

Fields in class1.Fields are variables.2.They can be primitives or references to objects.For example, the Employee class has two fields, age and salary.

public class Employee{  int age;  int salary}

Page 6: Simple class and object examples in java

Fields Naming Conventions

Page 7: Simple class and object examples in java

Fields Naming Conventions

1.Field names should follow the camel naming convention.

Page 8: Simple class and object examples in java

Fields Naming Conventions

1.Field names should follow the camel naming convention.

2.The initial of each word in the field, except for the first word, is written with a capital letter.

Page 9: Simple class and object examples in java

Fields Naming Conventions

1.Field names should follow the camel naming convention.

2.The initial of each word in the field, except for the first word, is written with a capital letter.

3.For example: age, maxAge, address, validAddress, numberOfRows.

Page 10: Simple class and object examples in java

Instance variables

Variables within a class but outside any method.

Page 11: Simple class and object examples in java

Instance Methods

Methods defined in a class which is only accessible through the Object of the class are called Instance methods.

Page 12: Simple class and object examples in java

Example: Person Class

class Person{ String name; int age;}

Page 13: Simple class and object examples in java

Example: Person Class

class Person{ String name; int age;}

Without methods

Page 14: Simple class and object examples in java

Example: Person Classclass keyword

class Person{ String name; int age;}

Without methods

Page 15: Simple class and object examples in java

Example: Person ClassName of class

class keyword

class Person{ String name; int age;}

Without methods

Page 16: Simple class and object examples in java

Example: Person ClassName of class

class keyword

Start of class

class Person{ String name; int age;}

Without methods

Page 17: Simple class and object examples in java

Example: Person ClassName of class

class keyword

Start of class

End of class

class Person{ String name; int age;}

Without methods

Page 18: Simple class and object examples in java

Example: Person ClassName of class

class keyword

Start of classData members of class

with default access(instance variable)End of

class

class Person{ String name; int age;}

Without methods

Page 19: Simple class and object examples in java

How to declare object?

   

 

Page 20: Simple class and object examples in java

How to declare object?

Person p1;

   

 

Page 21: Simple class and object examples in java

How to declare object?

Person p1;

//declare reference to object 

 

Page 22: Simple class and object examples in java

How to declare object?

Person p1;

//declare reference to object//Syntax: <classname> <objectname>

 

Page 23: Simple class and object examples in java

How to declare object?

Person p1;

//declare reference to object//Syntax: <classname> <objectname>

• It is simply a variable that can refer to an object.

Page 24: Simple class and object examples in java

How to declare object?

Person p1;

//declare reference to object//Syntax: <classname> <objectname>

• It is simply a variable that can refer to an object.

nullp1

Person p1;

Page 25: Simple class and object examples in java

Allocate memory

   

 

 

Page 26: Simple class and object examples in java

Allocate memory

p1 = new Person(); 

   

 

Page 27: Simple class and object examples in java

Allocate memory

p1 = new Person();

//allocate a Person object 

 

 

Page 28: Simple class and object examples in java

Allocate memory

p1 = new Person();

//allocate a Person object//Syntax: <objectname> = new <classname>();

 

 

Page 29: Simple class and object examples in java

Allocate memory

p1 = new Person();

//allocate a Person object//Syntax: <objectname> = new <classname>();

• The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.

 

Page 30: Simple class and object examples in java

Allocate memory

p1 = new Person();

//allocate a Person object//Syntax: <objectname> = new <classname>();

• The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.

• This reference is, the address in memory of the object allocated by new.

Page 31: Simple class and object examples in java

Allocate memory

p1 = new Person();

//allocate a Person object//Syntax: <objectname> = new <classname>();

• The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.

• This reference is, the address in memory of the object allocated by new.

p1 = new Person(); p1

name

age

Person object

Page 32: Simple class and object examples in java

Combination of these statements

Page 33: Simple class and object examples in java

Combination of these statements

Page 34: Simple class and object examples in java

Combination of these statements

Page 35: Simple class and object examples in java

Combination of these statements

Page 36: Simple class and object examples in java

Person p1 = new Person();

Combination of these statements

Page 37: Simple class and object examples in java

Person p1 = new Person();

Combination of these statements

//Syntax: <classname> <objectname> = new <classname>();

Page 38: Simple class and object examples in java

public class for Person class public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;

int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}

Page 39: Simple class and object examples in java

public class for Person class public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;

int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}

Instance variables are initialized with

object name qualifier

Page 40: Simple class and object examples in java

public class for Person class public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;

int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}

Instance variables are initialized with

object name qualifier

Syntax to set value in instance variable:-

<objectname>.<variablename> = <value>;

Page 41: Simple class and object examples in java

public class for Person class

Syntax to get value from instance

variable:-<variable> = <objectname>.<instance_variable_name>

public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;

int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}

Instance variables are initialized with

object name qualifier

Syntax to set value in instance variable:-

<objectname>.<variablename> = <value>;

Page 42: Simple class and object examples in java

Complete Programpublic class NewClass1{ public static void main(String args[]) {

Person obj1 = new Person();obj1.name=“ramesh";obj1.age=22;int a=obj1.age;System.out.println(a);System.out.println(obj1.name);

}}

class Person{ String name; int age;}

Page 43: Simple class and object examples in java

Output

Page 44: Simple class and object examples in java

In this example, barking(), hungry() and sleeping() are instance

methods.

Instance variables

Example 1: Dog Classclass Dog{

String breed;int age;String color;

void barking() {}void hungry() {}void sleeping() {}

}

Page 45: Simple class and object examples in java

Example 2: Stock class

Class Stock {

public commodity;public price;

public void buy (int no_of commodity) {}public boolean sale () {}

}

Page 46: Simple class and object examples in java

Instance variables

Example 2: Stock class

Class Stock {

public commodity;public price;

public void buy (int no_of commodity) {}public boolean sale () {}

}

Page 47: Simple class and object examples in java

In this example, buy(), and sale() are instance

methods.

Instance variables

Example 2: Stock class

Class Stock {

public commodity;public price;

public void buy (int no_of commodity) {}public boolean sale () {}

}

Page 48: Simple class and object examples in java

In this example, buy(), and sale() are instance

methods.

Instance variables

Example 2: Stock class

Class Stock {

public commodity;public price;

public void buy (int no_of commodity) {}public boolean sale () {}

}

Collectively, the methods and variables defined within a class arecalled members of the class.

Page 49: Simple class and object examples in java

Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

Page 50: Simple class and object examples in java

Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

With methods

Page 51: Simple class and object examples in java

Private instance variables cannot be accessed outside the class

Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

With methods

Page 52: Simple class and object examples in java

Private instance variables cannot be accessed outside the class

getData() and display() instance

methods are public and can be accessed outside

the class.

Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

With methods

Page 53: Simple class and object examples in java

Private instance variables cannot be accessed outside the class

getData() and display() instance

methods are public and can be accessed outside

the class.

Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

Methods inside class can access private data of class. In this case getData() and display() methods

are accessing private data.

With methods

Page 54: Simple class and object examples in java

Private instance variables cannot be accessed outside the class

getData() and display() instance

methods are public and can be accessed outside

the class.

Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

Methods inside class can access private data of class. In this case getData() and display() methods

are accessing private data.

They are defined inside

class not inside method

With methods

Page 55: Simple class and object examples in java

public class code for Person class

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData();

p1.display(); }}

NOTE: getData() and Display()

method cannot be called without

object qualifier.

Page 56: Simple class and object examples in java

public class code for Person class

Qualifier: Object name

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData();

p1.display(); }}

NOTE: getData() and Display()

method cannot be called without

object qualifier.

Page 57: Simple class and object examples in java

public class code for Person class

Dot operator

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData();

p1.display(); }}

NOTE: getData() and Display()

method cannot be called without

object qualifier.

Page 58: Simple class and object examples in java

public class code for Person class

Instance method because it is called

using object

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData();

p1.display(); }}

NOTE: getData() and Display()

method cannot be called without

object qualifier.

Page 59: Simple class and object examples in java

public class code for Person class

Instance method because it is called

using object

Dot operator

Qualifier: Object name

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData();

p1.display(); }}

NOTE: getData() and Display()

method cannot be called without

object qualifier.

Page 60: Simple class and object examples in java

Complete programimport java.util.Scanner;

class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData(); p1.display(); }}

Page 61: Simple class and object examples in java

A program can contain multiple classes but only one public

class(same name as file name) and contains main method

Complete programimport java.util.Scanner;

class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData(); p1.display(); }}

Page 62: Simple class and object examples in java

Creating and instantiating Person class object p1.

A program can contain multiple classes but only one public

class(same name as file name) and contains main method

Complete programimport java.util.Scanner;

class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData(); p1.display(); }}

Page 63: Simple class and object examples in java

Calling instance methods of

Person Class using p1 object.

Creating and instantiating Person class object p1.

A program can contain multiple classes but only one public

class(same name as file name) and contains main method

Complete programimport java.util.Scanner;

class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}

public class abc { public static void main(String args[]) { Person p1 = new Person();

p1.getData(); p1.display(); }}

Page 64: Simple class and object examples in java

Output

Page 65: Simple class and object examples in java

Members of class

Page 66: Simple class and object examples in java

Members of class

Members of class

Page 67: Simple class and object examples in java

Members of class

Members of class

Data members

Page 68: Simple class and object examples in java

Members of class

Members of class

Data members Methods

Page 69: Simple class and object examples in java

Members of class

Members of class

Data members

Instance data members

Methods

Page 70: Simple class and object examples in java

Members of class

Members of class

Data members

Instance data members

Static data members/

Class Variables

Methods

Page 71: Simple class and object examples in java

Members of class

Members of class

Data members

Instance data members

Static data members

Methods

Instance methods

Page 72: Simple class and object examples in java

Members of class

Members of class

Data members

Instance data members

Static data members

Methods

Instance methods

Static methods/

Class methods

Page 73: Simple class and object examples in java

Class diagram in UML

Page 74: Simple class and object examples in java

Class diagram in UML

UML class is represented by the diagram shown below. The diagram is divided into four parts:-

Page 75: Simple class and object examples in java

Class diagram in UML

UML class is represented by the diagram shown below. The diagram is divided into four parts:-

•The top section is used to name the class.

Page 76: Simple class and object examples in java

Class diagram in UML

UML class is represented by the diagram shown below. The diagram is divided into four parts:-

•The top section is used to name the class.

•The second one is used to show the attributes of the class.

Page 77: Simple class and object examples in java

Class diagram in UML

UML class is represented by the diagram shown below. The diagram is divided into four parts:-

•The top section is used to name the class.

•The second one is used to show the attributes of the class.

•The third section is used to describe the operations performed by the class.

Page 78: Simple class and object examples in java

Variable Types

A class can contain any of the following variable types.• Local variables: Variables defined inside methods,

constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

• Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

• Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.