Java Programming Tutorial Index

Java Object Oriented

This lesson mainly focused on Java object and class and described the features of Java object-oriented programming.



Java has following OOPS features:

In this lesson our primary focus on Objects and Classes.

What is Object?

In real-world an entity that has state and its behavior is known as an object.

For Example:

  • A Car is an object. It has states (name, color, model) and its behavior (changing gear, applying brakes).
  • A Pen is an object. Its name is Parker; color is silver etc. known as its state. It is used to write, so writing is its behavior.

In real-world object and software object have conceptually similar characteristics. In terms of object-oriented programming, software objects also have a state and behavior.

What is a class?

  • A class is a template or blueprint that is used to create objects.
  • Class representation of objects and the sets of operations that can be applied to such objects.
  • A class consists of Data members and methods.

The primary purpose of a class is to hold data/information. This is achieved with attributes which are also known as data members.

The member functions determine the behavior of the class, i.e. provide a definition for supporting various operations on data held in the form of an object.

Defining a Class in Java

Syntax:

public class class_name
{
    Data Members;
    Methods;
}

Example:

public class Car
{
    public:
    double color; // Color of a car
    double model; // Model of a car
}
  • Private, Protected, Public is called visibility labels.
  • The members that are declared private can be accessed only from within the class.
  • Public members can be accessed from outside the class also.

Class Members

Data and functions are members.

Data Members and methods must be declared within the class definition.

Example:

public class Cube
{
    int length; // length is a data member of class Cube
    int breadth; // breadth is a data member of class Cube
    int length ; // Error redefinition of length 
}
  • A member cannot be redeclared within a class.
  • No member can be added elsewhere other than in the class definition.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram