Object and Class in Java
Java is most secure and platform independent programming language, It is Object oriented programming language; Every program in java is based of object and class concept.
Since Java is an object oriented language, complete java language is build on classes and object. Java is also known as a strong Object oriented programming language(oops).
OOPS is a programming approach which provides solution to problems with the help of algorithms based on real world. It uses real world approach to solve a problem. So object oriented technique offers better and easy way to write program then procedural programming model such as C, ALGOL, PASCAL.
Main Features of OOPS
- Inheritence
- Polymorphism
- Encapsulation
- Abstraction
As an object oriented language Java supports all the features given above. We will discuss all these features in detail later.
Class
In Java everything is encapsulated under classes. Class is the core of Java language. Class can be defined as a template/ blueprint that describe the behaviors /states of a particular entity. A class defines new data type. Once defined this new type can be used to create object of that type. Object is an instance of class. You may also call it as physical existence of a logical template class.A class is declared using class keyword. A class contain both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.
Rules for Java Class
- A class can have only public or default(no modifier) access specifier.
- It can be either abstract, final or concrete (normal class).
- It must have the class keyword, and class must be followed by a legal identifier.
- It may optionally extend one parent class. By default, it will extend java.lang.Object.
- It may optionally implement any number of comma-separated interfaces.
- The class's variables and methods are declared within a set of curly braces {}.
- Each .java source file may contain only one public class. A source file may contain any number of default visible classes.
- Finally, the source file name must match the public class name and it must have a .java suffix.
- A simple class example
Suppose, Student is a class and student's name, roll number, age will be its property. Lets see this in Java syntax
class Student.When a reference is made to a particular student with its property then it becomes an object, physical existence of Student class.
{
String name;
int rollno;
int age;
}
Student std=new Student();
Reference: Object and Class in Java
No comments:
Post a Comment