SlideShare una empresa de Scribd logo
1 de 78
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, Regula
rRateCalculator.
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
1.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
}
Fields Naming Conventions
Fields Naming Conventions
1.Field names should follow the camel naming
convention.
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.
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.
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
class
keyword

Example: Person Class

class Person
{
String name;
int age;
}

Without
methods
class
keyword

Example: Person Class

class Person
{
String name;
int age;
}

Name of class

Without
methods
class
keyword

Example: Person Class

class Person
{
String name;
int age;
}

Name of class

Without
methods

Start of
class
class
keyword

Example: Person Class

class Person
{
String name;
int age;
}

Name of class

Without
methods

Start of
class
End of
class
class
keyword

Example: Person Class

class Person
{
String name;
int age;
}

End of
class

Data members of class
with default
access(instance variable)

Name of class

Without
methods

Start of
class
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.

Person p1;

null
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.
name

p1 = new Person();

p1

age
Person object
Combination of these statements
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();
//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
Instance variables
are initialized with
object name
qualifier

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);
Syntax to set value in
System.out.println(obj1.name);
instance variable:}
}
<objectname>.<variablename> = <value>;
public class for Person class
Instance variables
are initialized with
object name
qualifier

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);
Syntax to set value in
System.out.println(obj1.name);
instance variable:}
} Syntax to get value
<objectname>.<variablename> = <value>;

from instance
variable:<variable> = <objectname>.<instance_variable_name>
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;
}
Output
Example 1: Dog Class
class Dog
{
String breed;
int age;
String color;

Instance variables

void barking() {}
void hungry() {}
void sleeping() {}
}
In this example,
barking(), hungry() and
sleeping() are instance
methods.
Example 2: Stock class
Class Stock
{
public commodity;
public price;

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

Instance variables

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

Instance variables

public void buy (int no_of commodity) {}
public boolean sale () {}
}
In this
example, buy(), and
sale() are instance
methods.
Example 2: Stock class
Class Stock
{
public commodity;
public price;

Instance variables

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.

In this
example, buy(), and
sale() are instance
methods.
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
Example 3: Person Class
class Person
{
private String name;
private int age;

Private instance variables cannot be
accessed outside the class

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
Example 3: Person Class
class Person
{
private String name;
private int age;

Private instance variables cannot be
accessed outside the class

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

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;

Private instance variables cannot be
accessed outside the class

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

getData() and
display() instance
methods are
public and can be
accessed outside
the class.

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

Example 3: Person Class

class Person
{
private String name;
private int age;

Private instance variables cannot be
accessed outside the class

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

getData() and
display() instance
methods are
public and can be
accessed outside
the class.

Methods inside class can access
private data of class. In this case
getData() and display() methods
are accessing private data.
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
public class abc
{
public static void main(String args[])
{
Person p1 = new Person();
p1.getData();
p1.display();

}
}

Qualifier
: Object
name

NOTE: getData()
and Display()
method cannot be
called without
object qualifier.
public class code for Person class
public class abc
{
public static void main(String args[])
{
Person p1 = new Person();
p1.getData();
p1.display();

}
}

Dot operator

NOTE: getData()
and Display()
method cannot be
called without
object qualifier.
public class code for Person class
public class abc
{
public static void main(String args[])
{
Person p1 = new Person();
p1.getData();
p1.display();

}
}

Instance method
because it is called
using object

NOTE: getData()
and Display()
method cannot be
called without
object qualifier.
public class code for Person class
public class abc
{
public static void main(String args[])
{
Person p1 = new Person();
p1.getData();
p1.display();

}
}

Qualifier
: Object
name

Instance method
because it is called
using object

Dot operator

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 class abc
{
public static void main(String args[])
{
Person p1 = new Person();

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

p1.getData();
p1.display();
Complete program
import java.util.Scanner;
class Person
{
private String name;
private int age;

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

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

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 class abc
{
public static void main(String args[])
{
Person p1 = new Person();
p1.getData();
p1.display();

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

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 class abc
{
public static void main(String args[])
{
Person p1 = new Person();
p1.getData();
p1.display();

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

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
Calling instance
methods of
Person Class using
p1 object.
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/
Class 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.

Más contenido relacionado

La actualidad más candente

The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181Mahmoud Samir Fayed
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectOUM SAOKOSAL
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
The Ring programming language version 1.9 book - Part 84 of 210
The Ring programming language version 1.9 book - Part 84 of 210The Ring programming language version 1.9 book - Part 84 of 210
The Ring programming language version 1.9 book - Part 84 of 210Mahmoud Samir Fayed
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 
The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 56 of 88
The Ring programming language version 1.3 book - Part 56 of 88The Ring programming language version 1.3 book - Part 56 of 88
The Ring programming language version 1.3 book - Part 56 of 88Mahmoud Samir Fayed
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-conceptsraahulwasule
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180Mahmoud Samir Fayed
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)Shambhavi Vats
 

La actualidad más candente (20)

The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181The Ring programming language version 1.5.2 book - Part 70 of 181
The Ring programming language version 1.5.2 book - Part 70 of 181
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Core java oop
Core java oopCore java oop
Core java oop
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
The Ring programming language version 1.9 book - Part 84 of 210
The Ring programming language version 1.9 book - Part 84 of 210The Ring programming language version 1.9 book - Part 84 of 210
The Ring programming language version 1.9 book - Part 84 of 210
 
Object concepts
Object conceptsObject concepts
Object concepts
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
The Ring programming language version 1.3 book - Part 56 of 88
The Ring programming language version 1.3 book - Part 56 of 88The Ring programming language version 1.3 book - Part 56 of 88
The Ring programming language version 1.3 book - Part 56 of 88
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
 

Destacado (19)

Abstract classes and Methods in java
Abstract classes and Methods in javaAbstract classes and Methods in java
Abstract classes and Methods in java
 
What's database normalization
What's database normalizationWhat's database normalization
What's database normalization
 
Hardware concepts
Hardware conceptsHardware concepts
Hardware concepts
 
Inline functions in c++
Inline functions in c++Inline functions in c++
Inline functions in c++
 
open source
open sourceopen source
open source
 
open source
open sourceopen source
open source
 
Corruption In India
Corruption In IndiaCorruption In India
Corruption In India
 
Incredible India
Incredible IndiaIncredible India
Incredible India
 
My country india
My country   indiaMy country   india
My country india
 
Our Country India
Our Country IndiaOur Country India
Our Country India
 
Corruption in india
Corruption in indiaCorruption in india
Corruption in india
 
Black Money in India.
Black Money in India.Black Money in India.
Black Money in India.
 
Black money ppt
Black money pptBlack money ppt
Black money ppt
 
INCREDIBLE INDIA
INCREDIBLE INDIAINCREDIBLE INDIA
INCREDIBLE INDIA
 
Corruption ppt
Corruption pptCorruption ppt
Corruption ppt
 
India PPT
India PPTIndia PPT
India PPT
 
Solar power.ppt
Solar power.pptSolar power.ppt
Solar power.ppt
 
Solar energy power point presentation
Solar energy power point presentation Solar energy power point presentation
Solar energy power point presentation
 
Solar energy ppt
Solar energy pptSolar energy ppt
Solar energy ppt
 

Similar a Java class and object basics

03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
OOP with Java - continued
OOP with Java - continuedOOP with Java - continued
OOP with Java - continuedRatnaJava
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptxVishwanathanS5
 
38-object-concepts.ppt
38-object-concepts.ppt38-object-concepts.ppt
38-object-concepts.pptRavi Kumar
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 

Similar a Java class and object basics (20)

03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Oop
OopOop
Oop
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
OOP with Java - continued
OOP with Java - continuedOOP with Java - continued
OOP with Java - continued
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
javaClasses.pptx
javaClasses.pptxjavaClasses.pptx
javaClasses.pptx
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Basic object oriented concepts (1)
Basic object oriented concepts (1)Basic object oriented concepts (1)
Basic object oriented concepts (1)
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
 
38-object-concepts.ppt
38-object-concepts.ppt38-object-concepts.ppt
38-object-concepts.ppt
 
L02 Software Design
L02 Software DesignL02 Software Design
L02 Software Design
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 

Último

Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Último (20)

Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

Java class and object basics

  • 1. Simple class and object examples in Java Presented By Harish Gyanani
  • 2. What is class? A class is a blue print from which individual objects are created.
  • 3. Class naming convention • By convention, class names capitalize the initial of each word. • For example: Employee, Boss, DateUtility, PostOffice, Regula rRateCalculator.
  • 4. 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 ().
  • 5. Fields in class 1.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 }
  • 7. Fields Naming Conventions 1.Field names should follow the camel naming convention.
  • 8. 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.
  • 9. 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.
  • 10. Instance variables Variables within a class but outside any method.
  • 11. Instance Methods Methods defined in a class which is only accessible through the Object of the class are called Instance methods.
  • 12. Example: Person Class class Person { String name; int age; }
  • 13. Example: Person Class class Person { String name; int age; } Without methods
  • 14. class keyword Example: Person Class class Person { String name; int age; } Without methods
  • 15. class keyword Example: Person Class class Person { String name; int age; } Name of class Without methods
  • 16. class keyword Example: Person Class class Person { String name; int age; } Name of class Without methods Start of class
  • 17. class keyword Example: Person Class class Person { String name; int age; } Name of class Without methods Start of class End of class
  • 18. class keyword Example: Person Class class Person { String name; int age; } End of class Data members of class with default access(instance variable) Name of class Without methods Start of class
  • 19. How to declare object?
  • 20. How to declare object? Person p1;
  • 21. How to declare object? Person p1; //declare reference to object
  • 22. How to declare object? Person p1; //declare reference to object //Syntax: <classname> <objectname>
  • 23. How to declare object? Person p1; //declare reference to object //Syntax: <classname> <objectname> • It is simply a variable that can refer to an object.
  • 24. How to declare object? Person p1; //declare reference to object //Syntax: <classname> <objectname> • It is simply a variable that can refer to an object. Person p1; null p1
  • 26. Allocate memory p1 = new Person();
  • 27. Allocate memory p1 = new Person(); //allocate a Person object
  • 28. Allocate memory p1 = new Person(); //allocate a Person object //Syntax: <objectname> = new <classname>();
  • 29. 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.
  • 30. 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.
  • 31. 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. name p1 = new Person(); p1 age Person object
  • 32. Combination of these statements
  • 33. Combination of these statements
  • 34. Combination of these statements
  • 35. Combination of these statements
  • 36. Combination of these statements Person p1 = new Person();
  • 37. Combination of these statements Person p1 = new Person(); //Syntax: <classname> <objectname> = new <classname>();
  • 38. 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); } }
  • 39. 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
  • 40. public class for Person class Instance variables are initialized with object name qualifier 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); Syntax to set value in System.out.println(obj1.name); instance variable:} } <objectname>.<variablename> = <value>;
  • 41. public class for Person class Instance variables are initialized with object name qualifier 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); Syntax to set value in System.out.println(obj1.name); instance variable:} } Syntax to get value <objectname>.<variablename> = <value>; from instance variable:<variable> = <objectname>.<instance_variable_name>
  • 42. 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; }
  • 44. Example 1: Dog Class class Dog { String breed; int age; String color; Instance variables void barking() {} void hungry() {} void sleeping() {} } In this example, barking(), hungry() and sleeping() are instance methods.
  • 45. Example 2: Stock class Class Stock { public commodity; public price; public void buy (int no_of commodity) {} public boolean sale () {} }
  • 46. Example 2: Stock class Class Stock { public commodity; public price; Instance variables public void buy (int no_of commodity) {} public boolean sale () {} }
  • 47. Example 2: Stock class Class Stock { public commodity; public price; Instance variables public void buy (int no_of commodity) {} public boolean sale () {} } In this example, buy(), and sale() are instance methods.
  • 48. Example 2: Stock class Class Stock { public commodity; public price; Instance variables 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. In this example, buy(), and sale() are instance methods.
  • 49. 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); } }
  • 50. 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
  • 51. Example 3: Person Class class Person { private String name; private int age; Private instance variables cannot be accessed outside the class 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
  • 52. Example 3: Person Class class Person { private String name; private int age; Private instance variables cannot be accessed outside the class 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 getData() and display() instance methods are public and can be accessed outside the class.
  • 53. Example 3: Person Class class Person { private String name; private int age; Private instance variables cannot be accessed outside the class 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 getData() and display() instance methods are public and can be accessed outside the class. Methods inside class can access private data of class. In this case getData() and display() methods are accessing private data.
  • 54. They are defined inside class not inside method Example 3: Person Class class Person { private String name; private int age; Private instance variables cannot be accessed outside the class 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 getData() and display() instance methods are public and can be accessed outside the class. Methods inside class can access private data of class. In this case getData() and display() methods are accessing private data.
  • 55. 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.
  • 56. public class code for Person class public class abc { public static void main(String args[]) { Person p1 = new Person(); p1.getData(); p1.display(); } } Qualifier : Object name NOTE: getData() and Display() method cannot be called without object qualifier.
  • 57. public class code for Person class public class abc { public static void main(String args[]) { Person p1 = new Person(); p1.getData(); p1.display(); } } Dot operator NOTE: getData() and Display() method cannot be called without object qualifier.
  • 58. public class code for Person class public class abc { public static void main(String args[]) { Person p1 = new Person(); p1.getData(); p1.display(); } } Instance method because it is called using object NOTE: getData() and Display() method cannot be called without object qualifier.
  • 59. public class code for Person class public class abc { public static void main(String args[]) { Person p1 = new Person(); p1.getData(); p1.display(); } } Qualifier : Object name Instance method because it is called using object Dot operator NOTE: getData() and Display() method cannot be called without object qualifier.
  • 60. Complete program import java.util.Scanner; class Person { private String name; private int age; public class abc { public static void main(String args[]) { Person p1 = new Person(); } 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); } } p1.getData(); p1.display();
  • 61. Complete program import java.util.Scanner; class Person { private String name; private int age; public class abc { public static void main(String args[]) { Person p1 = new Person(); } 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); } } p1.getData(); p1.display(); A program can contain multiple classes but only one public class(same name as file name) and contains main method
  • 62. Complete program import java.util.Scanner; class Person { private String name; private int age; public class abc { public static void main(String args[]) { Person p1 = new Person(); p1.getData(); p1.display(); } 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); } } 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
  • 63. Complete program import java.util.Scanner; class Person { private String name; private int age; public class abc { public static void main(String args[]) { Person p1 = new Person(); p1.getData(); p1.display(); } 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); } } 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 Calling instance methods of Person Class using p1 object.
  • 67. Members of class Members of class Data members
  • 68. Members of class Members of class Data members Methods
  • 69. Members of class Members of class Data members Instance data members Methods
  • 70. Members of class Members of class Data members Instance data members Static data members/ Class Variables Methods
  • 71. Members of class Members of class Data members Instance data members Static data members Methods Instance methods
  • 72. Members of class Members of class Data members Instance data members Static data members Methods Instance methods Static methods/ Class methods
  • 74. Class diagram in UML UML class is represented by the diagram shown below. The diagram is divided into four parts:-
  • 75. 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.
  • 76. 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.
  • 77. 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.
  • 78. 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.