Wednesday, 21 June 2017

What is an Object and a Class

Objects:


Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

Real-world objects share two characteristics: They all have state and behavior. Cars have state (current gear, applying brakes and etc) and behavior (gear change, speeding and etc). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

These real-world observations all translate into the world of object-oriented programming. 

Class:


class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.

Below example illustrates more about an Object and a Class.

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of cars in existence, all of the same make and model. Each car was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your car is an instance of the class of objects known as cars. A class is the blueprint from which individual objects are created.


Lets see how a Java class and Objects implementation looks like. Don't worry about the syntactical implementation as you my be new to Java programming, just focus on the design how a Class with it's Objects look like.


Constructor:

A class contains constructors that are invoked to create objects from the class blueprint. Constructor is used to initialize it's class variables or call any method automatically without needing of invoking externally as soon as an instance is created. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:


package com.tutorial.sample;

class Car {

 private int speed;
 private int gearNo;

 //no-argument constructor
 public Car() {
  this.gearNo = 3;
  this.speed = 60;
 }
        
        //Parameterized constructor
 public Car(int gearNo, int speed) {
  this.gearNo = gearNo;
  this.speed = speed;
 }
public int getGearNo() { return gearNo; } public int getSpeed() { return speed; } } public class MainClass { public static void main(String[] args) { System.out.println("no-argument constructor"); Car car1 = new Car(); System.out.println("Gear: " + car1.getGearNo()); System.out.println("Speed: " + car1.getSpeed()); System.out.println("Parameterized construtor"); Car car2 = new Car(4, 70); System.out.println("Gear: " + car2.getGearNo()); System.out.println("Speed: " + car2.getSpeed()); } } Output: no-argument constructor Gear: 3 Speed: 60 Parameterized construtor Gear: 4 Speed: 70

To create a new Car object called car1, a constructor is called by the new operator:

Car car1 = new Car();

new Car() creates space in memory for the object and initializes its fields.

Car class has 2 constructor one with arguments and another with no-arguments.

Car car1 = new Car(); invokes the no-argument constructor to create a new Car object called car1.

Car car2 = new Car(4, 70);invokes the argument based constructor to create a new Car object called car2.

As like methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.

You don't have to provide any constructors for your class, the compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

You can use access modifiers in a constructor's declaration to control which other classes can call the constructor.

No comments:

Post a Comment