How to implement fundamental concepts of OOPs in java – Abstraction:

            Fundamental concepts of java includes the following.

  • ·       Abstraction
  • ·       Inheritance
  • ·       Polymorphism
  • ·       Encapsulation

Each one is coded with examples.

Abstraction:

              It is the concept of hiding the internal information and displays the necessary details of object.

First, create an abstract class vehicle with one abstract method without any definition.
Define a display() method with a printing statement.

Create two child classes for vehicle class as Car and Bus.

Write the definition for abstract method info()

Now, create a class “sampleabstract” which has the main method.

Create the objects for Car and Bus classes.

Call the functions by objects.

abstract class vehicle {

    // Abstract method with declaration

    abstract void info();

    // Normal method

    void display() {

        System.out.println("It displays a vehicle information");

    }

}

 

class Car extends vehicle {

    // Define the abstract method

    void info() {

        System.out.println("This is a car");

    }

}

 

class Bus extends vehicle {

    // Definition of abstract method

    void info() {

        System.out.println("This is a bus");

    }

}

public class sampleabstract {

    public static void main(String[] args) {

        vehicle Car = new Car();

        vehicle Bus = new Bus();

         Car.info();

        Bus.info();

        Car.display();

        }

}

Compile and run the program to display the result.

Here is the result.

C:\raji\blog>javac sampleabstract.java

C:\raji\blog>java sampleabstract

This is a car

This is a bus

It displays a vehicle information

This is the way of implementing Abstraction in java. Happy Coding!!!

No comments:

Post a Comment