Class and Object in Java

To understand the Class and Object in java, first you simply you imagine the real world. Suppose you have a house. In house we stay, before we stay we build a house. Before we build house, we design it. We make a sketch or blue print of that house. In that sketch we determine structure of house in form of architectural drawing. We all know that we can't stay in that architectural drawing. We have to built a house to stay in it. Here, class is like a sketch and the object is house. \

A class is like the architectural drawing of a house from which the house object is built.  Just as you can’t stay in an architectural drawing of a house, you cannot ‘stay’ in a class.  Just as someone has to build a house from its architectural drawing before you can  actually stay in it, you must build an object of a class before you can get a program to  perform the tasks defined in the class. In a housing estate, there are many houses built  from the same architectural design. Each of these houses will therefore contain the same  structure in spite of them being unique. In an object program, the same applies where there  can be more than one objects created from the same class, each are unique, and yet have  the same attributes and behaviours. When an object is created from a class, we say that the  object is an instance of the class. The term instantiation is used to refer to the process of  creating an object from a class.

Elements of class

Class is made from three major components, they are attributes, method and constructor. 

Attributes:- Attributes are used to store the properties of a object or state of object. For example, car is an object and the color, model etc are the properties of car. Attributes are represented in class by using primitive data types, object of another class and arrays. 

Method:-Methods are the task which the object can perform. For example, car can run, turn etc.

Constructor:- when an object is instantiated from a class, the constructor is automatically executed to initialize the attributes of object. To make a constructor in class, the name of constructor must be same to class name.

when the attributes, methods and constructor are declared in class then it is said the member of class.

Access modifiers

They defines the accessibility of  a particular class or class member.

Two level of access control: 

1) the class level
-public
-package-private

2) the member level
-public 
-private
-protected

public

The public access modifier is the most permissive access level. At the class level, a public class is visible to all classes everywhere. At the member level, there are no restrictions on accessing public members anywhere in the program.

Private

A private access modifier (only applicable in member level) on the other hand is the most restrictive access level whereby the code can only be access by codes in the same class.

Protected

A protected access modifier (only applicable in member level) denotes that the member is accessible from within its own package, the class in which it is declared, and from any subclasses derived from the class that declared this member.




Comments

Popular posts from this blog

Logical operator in JAVA

Loop Structure