Wednesday, 8 November 2017

Java Object and Classes

November 08, 2017 Posted by Prakash No comments

Java Object and Classes

This lesson mainly focused on Java object and class, and describes the features of Java object-oriented programming.
Java has following OOPS features:
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
  • Classes
  • Objects
  • Instance
  • Method
  • Message Parsing
In this lesson our major 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.
  • Class consists of Data members and methods.
Primary purpose of a class is to held data/information. This is achieved with attributes which is also known as data members.
The member functions determine the behavior of the class i.e. provide definition for supporting various operations on data held in 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
}
  • PrivateProtectedPublic are 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 redeclare within a class.
  • No member can be added elsewhere other than in the class definition.

0 comments:

Post a Comment