Simple class and object examples in java

gyananihrithik 1,229 views 78 slides Feb 12, 2014
Slide 1
Slide 1 of 78
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78

About This Presentation

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


Slide Content

Simple class and object examples in Java Presented By Harish Gyanani

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

Class naming convention By convention, class names capitalize the initial of each word. For example: Employee, Boss, DateUtility , PostOffice , RegularRateCalculator .

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

Fields in class Fields are variables. 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 }

Fields Naming Conventions

Fields Naming Conventions Field names should follow the camel naming convention.

Fields Naming Conventions Field names should follow the camel naming convention. The initial of each word in the field, except for the first word, is written with a capital letter.

Fields Naming Conventions Field names should follow the camel naming convention. The initial of each word in the field, except for the first word, is written with a capital letter. For example: age, maxAge, address, validAddress, numberOfRows.

Instance variables Variables within a class but outside any method .

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

Example: Person Class class Person { String name; int age; }

Example: Person Class class Person { String name; int age; } Without methods

Example: Person Class class keyword class Person { String name; int age; } Without methods

Example: Person Class Name of class class keyword class Person { String name; int age; } Without methods

Example: Person Class Name of class class keyword Start of class class Person { String name; int age; } Without methods

Example: Person Class Name of class class keyword Start of class End of class class Person { String name; int age; } Without methods

Example: Person Class Name of class class keyword Start of class Data members of class with default access(instance variable) End of class class Person { String name; int age; } Without methods

How to declare object?

How to declare object? Person p1;

How to declare object? Person p1; //declare reference to object

How to declare object? Person p1; //declare reference to object //Syntax: <classname> <objectname>

How to declare object? Person p1; //declare reference to object //Syntax: <classname> <objectname> It is simply a variable that can refer to an object.

How to declare object? Person p1; //declare reference to object //Syntax: <classname> <objectname> It is simply a variable that can refer to an object. null p1 Person p1;

Allocate memory

Allocate memory p1 = new Person();

Allocate memory p1 = new Person(); //allocate a Person object

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

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.

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 .

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

Combination of these statements

Combination of these statements

Combination of these statements

Combination of these statements

Person p1 = new Person(); Combination of these statements

Person p1 = new Person(); Combination of these statements //Syntax: < classname > < objectname > = new < classname >();

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

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

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

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

Complete Program 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 ); } } class Person { String name ; int age; }

O utput

 In this example, barking(), hungry() and sleeping() are instance methods . Instance variables Example 1: Dog Class class Dog { String breed; int age; String color; void barking() {} void hungry() {} void sleeping() {} }

Example 2: Stock class Class  Stock   { public commodity; public price ; public void buy ( int no_of commodity) {} public boolean sale () {} }

Instance variables Example 2: Stock class Class  Stock   { public commodity; public price ; public void buy ( int no_of commodity) {} public boolean sale () {} }

 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 () {} }

 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 are called members of the class.

Example 3: Person Class 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); } }

Example 3: Person Class 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); } } With methods

Private instance variables cannot be accessed outside the class Example 3: Person Class 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); } } With methods

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 Class 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); } } With methods

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 Class 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); } } Methods inside class can access private data of class. In this case getData () and display() methods are accessing private data. With methods

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

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.

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.

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.

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.

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.

Complete program import 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(); } }

A program can contain multiple classes but only one public class(same name as file name) and contains main method Complete program import 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(); } }

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 program import 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(); } }

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 program import 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(); } }

Output

Members of class

Members of class Members of class

Members of class Members of class Data members

Members of class Members of class Data members Methods

Members of class Members of class Data members Instance data members Methods

Members of class Members of class Data members Instance data members Static data members/ Class Variables Methods

Members of class Members of class Data members Instance data members Static data members Methods Instance methods

Members of class Members of class Data members Instance data members Static data members Methods Instance methods Static methods/ C lass methods

Class diagram in UML

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

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 .

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 .

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.

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 .