Java Keywords

 InstanceOf

It is a keyword used to test whether an object is an instance of a particular class, interface, subclass of a given type.

Example 01:

package com.java.Keyword;


class Shape_1 {

void draw() {

System.out.println("Drawing a generic shape");

}

}


class Circle_1 extends Shape_1 {

void draw() {

System.out.println("Drawing a circle");

}

}


class Rectangle_1 extends Shape_1 {

void draw() {

System.out.println("Drawing a rectangle");

}

}




public class InstanceOfExample {

public static void main(String[] args) {

Shape_1 shape1 = new Circle_1();

Shape_1 shape2 = new Rectangle_1();

Shape_1 shape3 = new Shape_1();


drawShape(shape1);

drawShape(shape2);

drawShape(shape3);

}




static void drawShape(Shape_1 shape) {

if (shape instanceof Circle_1) {

Circle_1 circle = (Circle_1) shape; // Cast to Circle to access specific methods

System.out.println("This is a Circle:");

circle.draw();

} else if (shape instanceof Rectangle_1) {

Rectangle_1 rectangle = (Rectangle_1) shape; // Cast to Rectangle to access specific methods

System.out.println("This is a Rectangle:");

rectangle.draw();

} else {

System.out.println("This is a generic Shape:");

shape.draw();

}

}

}

Super Keyword

It is a reference variable that refers to the immediate parent class objects.

Example 02:

package com.java.Keyword;

class Vehicle {

int speed;


Vehicle(int speed) {

this.speed = speed;

}


void displaySpeed() {

System.out.println("Vehicle speed: " + speed + " km/h");

}

}


class Car extends Vehicle {

int additionalSpeed;


Car(int speed, int additionalSpeed) {

super(speed); // calling the constructor of the superclass

this.additionalSpeed = additionalSpeed;

}


void accelerate() {

speed += additionalSpeed;

System.out.println("Car is accelerating. New speed: " + speed + " km/h");

}


void displayCarSpeed() {

super.displaySpeed(); // invoking the superclass method

System.out.println("Car additional speed: " + additionalSpeed + " km/h");

}

}


public class SuperKeywordExample {

public static void main(String[] args) {

Car myCar = new Car(60, 30);

myCar.displayCarSpeed();

myCar.accelerate();

myCar.displayCarSpeed();

}

}


Strictfp Keyword

This is applied to all the classes, interfaces, and methods. The purpose is to ensure consistent and predictable floating points across arithmetic platforms.

 Example 03:

package com.java.Keyword;

public class StrictfpExample {


// Method with strictfp modifier

strictfp static double strictfpMethod() {

double result = 10.0 / 3.0;

return result;

}


// Method without strictfp modifier

static double nonStrictfpMethod() {

double result = 10.0 / 3.0;

return result;

}


public static void main(String[] args) {

// Calling methods and printing results

double strictfpResult = strictfpMethod();

double nonStrictfpResult = nonStrictfpMethod();


System.out.println("Result from strictfpMethod: " + strictfpResult);

System.out.println("Result from nonStrictfpMethod: " + nonStrictfpResult);

}

}



Covariant data type:
Using this concept we can change the data type of the overridden method.

package Methods;

class Base {

Base create() {

System.out.println("Base::create() called");

return new Base();

}


void print() {

System.out.println("Base class");

}

}


class Derived extends Base {

// Covariant return type

@Override

Derived create() {

System.out.println("Derived::create() called");

return new Derived();

}


@Override

void print() {

System.out.println("Derived class");

}

}


public class CovariantReturnTypeExample {

public static void main(String[] args) {

// Create a Derived object and assign it to a Base reference

Base base = new Derived();


// Call the overridden create() method with covariant return type

Derived derivedObject = (Derived) base.create();

// Output will be "Derived::create() called"

// Call the overridden print() method with dynamic dispatch

base.print();

// Output will be "Derived class" because the dynamic type is Derived


// You can also call the methods directly on the Derived object

derivedObject.print();

// Output will be "Derived class"

}

}


Post a Comment

0 Comments