Рет қаралды 7,934
#1 Java for Beginners | class and object
About this video:
This tutorial will explain concept of class and object with real life Point of view and with programming point of view too..
Timestamp:
0:40 What is a class?
1:34 What is an object?
3:43 How to create a class?
4:16 Syntax to Create a class
7:11 How to create an object?
7:25 Syntax to create an object
9:13 Example
Links:
To learn about basics of Java click here 👇
• Java Basics
Java Methods :
• #2 Method in Java | Ja...
Java variable declaration and initialization:
• #4 Variable Declaratio...
Java Constructor :
• #5 Java Constructor | ...
NOTES :
1.What is a class?
2.What is an object?
3.How to create a class?
4.How to create an object?
1.What is a class?
Class is a prototype from which objects can be created.
Class can also be defined as
The collection of objects because,you can create multiple objects using single class.
2.What is an object?
Object is instance of a class..
This means, object is a result that we get from a class..
And each object has it's fields /characteristics and actions / functions
For example,
Mobile Phone,
Mobile Phone is also an object..
And each mobile phone has
It's fields like,
colour, height, screenSize.
And actions like,
calling,ringing,sendData, receivedData etc.Remember one thing about class and object..
When we execute/ run the program then first program will load into memory and then execute
That means, first program will occupy some space into memory and then execute
Similarly,
In java,
During the execution of program,
first program load into memory this means will occupy some space into memory and then execute but class won't occupies any space into memory during the execution of program because
Class is logical entity.
So Class is work as file in that we have to put our logic
hence class will just be saved as a file on hard drive.
But object occupies memory during the execution of program because object is physical entity because
each object has it's actions.
This means each object can perform certain task.
Such as, Mobile Phones
Mobile Phone is also one of an object and it also has it's actions like,
Calling,ringing,send data, received data etc.
So basically,
In java, object is considered as a physical entity and class is considered as a logical entity.
3.How to create a class?
To create a class you can use following Syntax
AccessModifier ClassKeyword ClassName
{
// Class Members;
}
Access-modifier:
access - modifier controls the access level/ scope of a class.
In java, there are four type of access modifier.It's public,private,protected,and default but you can use only two access modifier for a class.It's public and default
If you declared class with
modifier public then class is accessible or visible to all classes in a program and if a class has no modifier then it's considered as a default and it is visible only within its own package and won't visible to other packages.
NOTE:Package is a group of related classes.
class-keyword:
Class keyword is used to create a class.
Class keyword is predefined keyword / reserved keyword.
Class keyword should be in lower-case otherwise compiler will raise an error because, Java is
a case-sensitive language.
Class-Name:
Class name should always start with upper case letter.
Class-Body:
Class body surrounded by curly braces.
Class body contain class members and class members are fields and actions so fields are called as Variables and actions are called as Methods.
For example:
public class Test
{
//declaring variables
int var1;
int var2;
//declaring methods
public void display (){}
}
In this example,
public is access modifier and Test is class name.
var1 and var2 are variables (fields) and display() is method (actions)
4.How to create an object?
To create an object you can use following syntax
Syntax:
Class_Name object_name = new_keyword Class_Name();
new-keyword:
"new" keyword is used to create an object
It is a predefined keyword that java provide us to create an object and
during the creation of an object compiler immediately allocate memory to an object and hence "new" keyword is also used to allocate memory to an object..
"new" keyword followed by class name and parantheses it represents the constructor of the class.
Constructor is a special method that has same name as that of a class.
Constructor is used to give initial value to an object.
That means when object creation take place then compiler immediately allocate memory to an object and give initial value(default values) to an object.
NOTE:Default values might be 0 or null.
For example:
You can create an object as follows,
Test obj = new Test();
here,
Test is class name,obj is object name and Test() is constructor.
_________End___________
Thank you for watching 🙏
#javaclassandobject #classandobjectinjava #sgtutorial #javatutorial #javatutorialforbegginers #java #classinjava #objectinjava #class #object