Class and object in java
By now you might have understood the basic concept of Object Oriented Programming. Now let us talk about what exactly Objects are:
Objects can be defined as a unique entity which contains data and functions (characteristics and behavior) together in an Object Oriented Programming (OOP) Language.
Let us consider any real-world object: Suppose a Chair
- It has four legs.
- It has two armrests.
- It has a seat, which may be plain or foamed.
- It comes in various colors.
- It is used to sit.
- It can be used to move 180° while sitting in the same place.
These characteristics and behaviors of a real-world object are equivalent to the data members of a Software world object.
Suppose if we are creating a database of all the residents of India: Then the data members of an Indian would be:
- Date of Birth
- Aadhar Number, PAN Card Number, Passport Number
- Blood Group, Biometrics and Genetics Information
These may be the data members of the Software Object ‘Indian’. The functions of the Object can be:
- To accept data: acceptdata() where values of the data members are accepted.
- To search for data in the database: searchdata().
- To display the data members of any individual: display()
A class is defined as the set of similar objects. Each object of a class possesses the same attributes and common behavior defined within the same class.
This may sound a bit of technical and complex.
So, let’s understand this in a simpler way:
Let us take the example of a car showroom:
A car showroom has various kinds of cars: high-end luxuries like Mercedes, Lamborghini, BMW, etc. , affordable SUVs like Renault, Scorpio, or small budget cars like i10, Alto, Kwid, etc.
Here each car has at least one common feature: It can be driven for transportation. Some may run on Petrol and others on Diesel.
Each car might have some features that are exclusive in it.
Also each car has a Vehicle No., Chassis No., Model Name, Size Color, etc.
So we can consider Car Showroom as a collection of cars.
If we represent this in a computer: The Car Showroom is a class, similar objects are the cars where each car has similar features and some exclusive features. These cars have same attributes: Model Name, Chassis No., etc (though the values are different) and a common behavior: Transportation.
Compare this with the definition of class, hope this is clear!!
Java thus is not only an object-oriented programming language but has many features similar to C++ as well.
It was originally created so that Internet applets can be executed and downloaded while Web Browsing but has rapidly experienced a surge in wide acceptance among the users as a programming language.