Class in Java
Class in Java
We know that java is an Object Oriented Programming Language. Whole java language functions on the concept of classes and objects. Classes can be defined as a blueprint for making an object. Let us understand what a class is through a real life example.
A car is a class. Swift DZire will be an object of the class car. It has attributes such as color, weight etc. and methods such as brake, drive, etc. It can be defined as a medium that allows the user to specify all attributes and methods that internally define the state and the behavior of the object.
To summarize we can say that : ‘A class is the basis of all computation in Java. ‘
Syntax
<access_modifier> class <ClassName>
{
//Class body containing objects, variables and methods
}
public class Employee { String name, city, state; int age, phn_num; void display() { //body of the code; } }
Some features of class:
- A class can be imagined as a big data type, hence it’s functionality is as good as a data type.
- An outer class should be either public or default so that it can be accessed by the JVM.
- Public: public class
- Default: class
- A class can have:
- Methods
- Variables
- An object that can access these variables and methods.
- A class represents the common properties and actions of an object
- It has the following set of access modifiers that control the access or the visibility of a class:
- Public: restricts the access to the class itself. Only methods that are part of the same class can access private members.
- Private: restricts the access to the class itself. Only methods that are part of the same class can access private members.
- Protected: allows the class itself and all its subclasses to access the member.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment