Tutorials Hut

Java Objects and Classes

Java is an object oriented programming language, everything in the Java world is object. Java supports a lot of fundamental concepts of object oriented programming language. If you have ever worked on C++ or any other object oriented programming language, a lot of concepts and features will remain same or similar.

Few OOPs concepts supported by java:

  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation

Few basic fundamentals of java:

  • Class
  • Object
  • Methods (Methods are called functions in many languages)
  • Instance
  • Attribute(Variable)

In this article we are going to discuss:

1. Class – Class is a skeleton or blueprint of an object, example Car class in java will be a blueprint of a real world car as well as while creating an object in java this will act as a blueprint. 

2. Object – Object in java is an instance of any java class, example if we have a Car class in java and we create objects car1, car2 and many more with help of Car class these all will be called objects.

Along with the above we will see few simple java programs to better understand these concepts in detail.

Java Classes

As discussed, classes are a blueprint of a real world or java world object from which we can create objects of class and based on our requirement change or use its behaviours.

Any class in java will always have two things which are known as attributes of a class:

  • Method – Method or function is a behaviour of class for example one method of car can be start()  to start and another can be stop() to stop the car.

  • Variable – Variable is a property of a class which may have a predefined value or can be assigned later by the object of class for example color, year, model, minSpeed, maxSpeed are variables of a class.

Types of variables:

  1. Class level variable: Any variable that is declared just below the class with a static keyword.
  2.  Local variable: Any variable declared within a method.
  3.  Instance variable: Any variable declared inside class (not in method without static keyword).

Below is an example of Car class, this will be saved in a file named Car.java, you can see the methods and variables which we discussed above.

public class Car {
String color;
int year;
String model;
int maxSpeed;
int minSpeed;

void start() {
}

void stop() {
}

void drive() {
}
}
java classes public key word

Java Objects

Object java is nothing but an instance of any java class, in above example we saw Car class and its variables and methods, now if we create a instance of that class it will be called object.

How to create an object? it is very simple, see below code where we created object of above class:

Code:

Car car1 = new Car()

In this car1 is an object of Car class, this is one of the simplest ways to create an object of class in java.

Constructors

Every java class has a constructor, constructors are used to create a new object and initialize variables with some value.

Constructors look like a function but their name is always same as class.

If a developer or programmer does not create own constructor then the compiler creates one for it which is not visible in class but used to create class objects and this type of constructor is called default constructor. 

Example of constructor:

In the simple class Car, code public car(){} is a constructor without any argument. 

public class Car {

String color;

public Car(){ //Constructor without arguments

}

}

Creating object

As discussed previously, the object is a blue print of class and so each object is constructed from a class. 

There are many ways to create constructors and the most used and simplest way is by uing new keyword.

A Simple Syntax to Create Class Using new Keyword:

<name of class> <object name, can be anything> = new <class name>(<arguments>)

See below example of class and object creation:

 

public class Car {

String color;

int maxSpeed = 100

public Car(){ //Constructor without arguments

}

public Car(String color){ //Constructor with arguments

System.out.println("Passed argument is: " + color);

}

public void startCar() {

System.out.println("Car started using method startCar”);

}

public static void main(String []args) {

// Below line of code will create an object car for Car class

Car car= new Car( "red" );

}

}

Object is created by this line of code in above example.

      Car car= new Car( “red” );

Compile and run above code, below output will come:

Passed argument is: red

Accessing Variables and Methods in a Class

By help of created objects we can access methods and variables inside a class.

Simple syntax to access variable:

<object>.<variable name>

Simple syntax to access method of class:

<object>.<method name>(<arguments>)

So we will take example of the above mentioned class Car and create objects, access variables and methods.

Example code:

public class Car {
   String color;
   int maxSpeed = 100
   public Car(){ //Constructor without arguments
   }
   public Car(String color){ //Constructor with arguments
	System.out.println("Passed argument is: " + color);
   }
   public void startCar() {
  System.out.println("Car started using method startCar”);
}
public static void main(String []args) {
// Below line of code will create an object car for Car class
      Car car= new Car( "red" );
//Below line of code will access maxSpeed variable
      System.out.println("Car’s max speed is:”+car.maxSpeed)
//Below line of code will access startCart() method
      car.startCar()      
   }
}

Output of above program:

Passed argument is:red
Car’s max speed is:100
Car started using method startCar

Package in Java and usage

A package is a folder or directory in simple words which contains related classes, so if you want to keep some similar type of classes in one bundle then can create them in a package.

Import in Java and usage

Import is used in many programming languages to bring the features of other classes or programs to the class which imports them by using import statements.

Example of package and import

import java.util.ArrayList;
class MyColors{
    public static void main(String[] args) {
        ArrayList myList = new ArrayList<>(3);
        myList.add(“R”);
        myList.add(“White”);
        myList.add(“Black”);
        System.out.println(myList);
    }}

In above example packages are:

com.java.colors
java.util

In above example impot is:

import java.util.ArrayList;

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *