on Leave a Comment

Object and Classes in Java

Java is an object-oriented programming language. Java programs are completely built by object and classes. A java program has at least one class and main() function is also written in a class. Java is a strong object-oriented programming language.

Features of OOPS

- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Classes
- Objects
- Instance
- Method
- Message Parsing

What is Object?

An object is a real world entity that some properties and methods. For example, a car has some properties like name, color and brand etc and some behaviour like brakes, gears etc.

Example:

Car c1 = new Car();

Here c1 contains reference of object of class Car. In java, objects always create using new keyword.

What is Class?

class is a blueprint of an object. In java, all properties and methods of object is encapsulated in class. A class defines which operation can be performed on an object. A class contains data members and method for object. A class can be of any access modifier. 

Syntax for defining class:

access-modifier class class-name
{
        //Data Members
        //Methods
}

Example:

public class Car
{
    String name;
    String color;
    String brand;

    void brakes()
    {
        //Some code here
    }
    void gear()
    {
        //Some code here
    }
}

Simple Program to Understand Object and Classes in Java

public class FirstProgram
{
void print()
{
System.out.println("Java is a simple language");
}
public static void main(String[] args)
{
FirstProgram f = new FirstProgram();
f.print();
}

}

OUTPUT:

Java is a simple language


0 comments:

Post a Comment