0% found this document useful (0 votes)
18 views18 pages

2.1.ppt Java

The document provides an overview of Java programming fundamentals, focusing on classes and objects in object-oriented programming. It explains the definitions, characteristics, and methods for declaring and initializing classes and objects, as well as creating and using methods. Additionally, it includes examples of class and object creation, along with various ways to initialize objects in Java.

Uploaded by

sneha chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views18 pages

2.1.ppt Java

The document provides an overview of Java programming fundamentals, focusing on classes and objects in object-oriented programming. It explains the definitions, characteristics, and methods for declaring and initializing classes and objects, as well as creating and using methods. Additionally, it includes examples of class and object creation, along with various ways to initialize objects in Java.

Uploaded by

sneha chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Java programming

Mrs. Sneha D. Patil


(Assistant Professor,
E&C)
INDEX
 CLASS FUNDAMENTALS
1. CLASS
2. OBJECTS
 CLASS DECLARATION
 OBJECT DECLARATION
 INITIALIZATION OF OBJECT
 METHODS
1. METHOD CREATION
 ACCESSING CLASS OBJECTS
 ASSIGNING OBJECT REFERENCE VARIABLES
CLASS FUNDAMENTALS
Objects and Classes in Java

 In object-oriented
programming technique, we
design a program using
objects and classes.
 An object in Java is the
physical as well as a logical
entity, whereas, a class in
Java is a logical entity only.
 An object has three
characteristics:
 An object is an instance of a class.

 A class is a template or blueprint from which objects are created.

 So, an object is the instance(result) of a class.

 Object Definitions:

• An object is a real-world entity.


• An object is a runtime entity.
• The object is an entity which has state and behavior.
• The object is an instance of a class.
CLASS

 A class is a group of
objects which have
common properties.
 It is a template or
blueprint from which
objects are created.
 It is a logical entity.
 It can't be physical.
CLASS DECLARATION

Syntax to declare a
class:
class <class_name>
{
field;
method;
}
Object Declaration
 The object is a basic building block
of an OOPs language. In Java, we
cannot execute any program
without creating an object.
Ways to initiate object

 1. Using new Keyword  2. Using


 When we create an instance of Class.forName(String
the class by using the new className) method
keyword, it allocates memory
 // creating object of public
(heap) for the newly
created object and also returns class Test
the reference of that object to // consider class Test present
that memory. in com.p1 package
 The new keyword is also used to  Test obj =
create an array.
(Test)Class.forName("com.p
 The syntax for creating an object 1.Test").newInstance();
is:
ClassName object = new ClassName(
);
Ways to initiate object

 3. Using clone() method  4. Deserialization


 clone() method is present in  De-serialization is a
the Object class. It creates technique of reading an
and returns a copy of the object from the saved state
object. in a file.
 // creating object of class  FileInputStream file = new
Test FileInputStream(filename);
Test t1 = new Test();  ObjectInputStream in = new
 // creating clone of above ObjectInputStream(file);
object  Object obj = in.readObject();
Test t2 = (Test)t1.clone();
Methods

 A method is a block of code which


only runs when it is called.
 You can pass data, known as
parameters, into a method.
 Methods are used to perform
certain actions, and they are also
known as functions.
Create a Method

 The method declaration


provides information
about method attributes,
such as visibility, return-
type, name, and
arguments. It has six
components that are
known as method header.
Example of class and object creation

public class CreateObjectExample1


{
void show()
{
System.out.println("Welcome to third year");
}
public static void main(String[] args)
{
//creating an object using new keyword
CreateObjectExample1 obj = new CreateObjectExample1();
//invoking method using the object
obj.show();
}
}
MAIN OUTSIDE THE CLASS

/Java Program to demonstrate having the main method in


//another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Ways to initialize object

 There are 3 ways to initialize object in Java.


 By reference variable
 By method
 By constructor
Assigning reference variable to object
Initialization through reference
class Student{
class Student
int id;
{
String name;
int id;
}
String name; class TestStudent3{
} public static void main(String args[]){
class TestStudent2{ Student s1=new Student();
public static void main(String args[]){ Student s2=new Student();
s1.id=101;
Student s1=new Student();
s1.name="Sneha";
s1.id=101;
s2.id=102;
s1.name="Sneha"; s2.name=“sonal";
System.out.println(s1.id+" "+s1.name);// System.out.println(s1.id+" "+s1.name);
} System.out.println(s2.id+" "+s2.name);
} }
}
Initialization through method
class Student class TestStudent4
{ {
int rollno; public static void main(String a
String name; rgs[]){

void insertRecord Student s1=new Student();

(int r, String n) Student s2=new Student();

{ s1.insertRecord(111,“sneha");

rollno=r;
s2.insertRecord(222,“sonal");
name=n; }
s1.displayInformation();
void displayInformation() {
s2.displayInformation();
System.out.println(rollno+" "+
} }
name);} }
To be continued……..
Next lecture

You might also like