OBJECT ORIENTED PROGRAMMING THROUGH JAVA
CLASSES AND OBJECTS
class:
class is a language construct (not an object oriented feature) i.e. similar to a structure
in ‘c’
language as long as user defined data type is concerned.
A class is defined as collection of similar objects.
Classes are user-defined data types and behave like the built-in types of a
programming language.
Class declaration contains name of the class and body of the class. The body of the
class may
consist of several statements and is enclosed between the braces {}.
Object:
Object is nothing but the copy of class. It will literally store RAM.
A class is a plan/blueprint for the proposed objects.
Object implements Encapsulation where as class is the basis of Encapsulation.
Class is the language feature, where as Encapsulation is the object oriented feature.
By writing
classes in programs we are implementing the concept Encapsulation.
Class Members
The class members are declared in the body of a class. These may comprise data
(variables in a
class). code (methods) and nested classes.
The members of a class comprise the members declared in the class as well as the
members
inherited from a super class. The scope of all the members extends to the entire class
body. The
fields comprise two types of variables
1. Non Static variables: These include instance and local variables
(a) Object Variables (Instance variables): These variables are individual to an object
and an object keeps a copy of these variables in its memory.
(b) Local variables: These are local in scope and not accessible outside their scope.
2. Class variables (Static Variables): These variables are also qualified as static
variables. The
values of these variables are common to all the objects of the class. The class keeps
only one
copy of these variables and all the objects share the same copy. As class variables
belong to
the whole class, these are also called class variables.
Declaration of Class Objects
Creating an object is also referred to as instantiating an object.
Objects in java are created dynamically using the new operator.
The new operator creates an object of the specified class and returns a reference
to that object.
Syntax: (creating an object)
Class Name object Reference=new class Name();
Access Control for Class Members
Access specifiers in java
These are used to define the scope of a variable or a method or a class.
There are three access specifiers in java
There are four accessibility modes in java.
private
default
protected
public
private
This mode has the visibility class level visibility.
default
This mode visibility is class level+ package level.
protected
This mode visibility class level + package level + child class level.
public
Total java Environment
Example:
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
[Link]([Link]);
[Link]([Link]);
}
}
Output:
0
null