What are Classes in Java ?
Actually, in JAVA the whole programming logic is based on classes only, there is no java program without class.
Class is the basic concept of OOP's. Now here's a question,Â
What is OOP?
-> OOP stands for Object Oriented Programing.
-> Object Oriented Programming is about to creating objects that contains both data(variables) and functions.
-> In OOP Class and Objects are main aspects.
Advantages of OOP:-
1. It is faster and easier to execute programs with Classes and Objects.
2. OOP helps to keep the code DRY (i.e., "Don't Repeat Yourself"), and makes the code easier to maintain, modify and debug.
3. OOP makes it possible to create full reusable applications with less code and shorter development time.
4. It provides a clear structure to the program.
Definition of Class:-
--> Class is a User-defined datatype . A class has some attributes and some functions. These are called as members of class. It works as a 'blueprint' for class objects.
-->Example:-
--> In Programming terms attributes are called as variables and functions are called as functions only.
-->In computer science, an object can be a variable, a data structure, a function, or a method, and as such, is a value in memory referenced by an identifier.
-->While declaring a class a "class" keyword is to be used and while declaring member functions and variables inside the class some access specifiers such as "public", "private", "protected" need to be specified.
-->By default the access specifier for all members is "private". Which means all members of class can be accessed only by class members not by other members which outside of the class.Â
--> Objects can contain data and code:Â
data in the form of fields (often known as attributes or properties or variables), and code, in the form of procedures (often known as methods or functions).
Syntax to Declare Class in Java :-
package com.company
// Declaring class as publicly
// Here access specifier are declared along with the class name
class class_name
{
// Here declare member variable
// Now declare member functions preceding with access specifier
//example
public static void main(String[] args)
{
//calling member variables by creating object.
//Syntax to create object.
class_name object_name = new class_name();
}
}
Syntax to create Objects of Class (calling class in main function) :-
Example :-
package com.company;
class Main
{
private
int a=10;
int b=154;
public static void object()
{
Main obj = new Main();
Main obj2 = new Main();
System.out.println(obj.a);
System.out.println(obj2.b);
}
public static void main(String[] args)
{
System.out.println("The numbers are :");
object();
}
}
Output
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP