How to implement OOPs concepts in java???

    ‘OOP’-Object Oriented Programming. It is the way of creating a paradigm in efficient manner. It also secures the code. In java, this is important part of programming.

What are the core concepts of java?

The  core concepts of java is listed below…

Class : Class  declares data and member functions. It is a blueprint of object.

Object : It is a real world entity. Eg: ‘Apple’

Abstraction: It hides the implementation.

Encapsulation :It wraps the data and functions into a single unit.

Polymorphism: Same member function have more than one definition.

Inheritance :It creates parent-child relationship.

How to implement OOPs concepts in java?

  • ·       First, create a class “fruit”.  Add an attribute  “fname” and create an method “display()”.
  • ·       Next, create a public class “fruitcls” and save this file as “fruitcls.java”
  • ·       Add the main() function
  • ·       Create a new object for fruit class.
    • fruit f1= new fruit();
  • ·       Assign the value for attribute fname as “Apple”
  • ·       Call the function display by using the object f1.display()

Here, the program begins…

class fruit {

    // Attribute

    String fname;

     // Method

    void display() {

        System.out.println("Fruit Name: " +fname);

        }

}

 public class fruitcls {

    public static void main(String[] args) {

        // Create an object of the fruit class

        fruit f1 = new fruit();

        // Assign values to the object's attributes

        f1.fname = "Apple";

        f1.display();

        }

}

Compile and execute the program to get the output.

C:\raji\blog>javac fruitcls.java

C:\raji\blog>java fruitcls

Fruit Name: Apple

Try a new class for display flower information.

Attributes: flowername, color

Methods: display()

 class flower {

    // Attributes

    String flowername;

    String color;

     // Method

    void display() {

        System.out.println("Flower Name: " +flowername);

        System.out.println("Color: " +color);

        }

}

 public class flowercls {

    public static void main(String[] args) {

        // Create an object of the flower class

        flower f1 = new flower();

                // Assign values to the object's attributes

        f1.flowername = "Senkanthal";

        f1.color = "Red with Orange";

        f1.display();

        }

}

The output is

C:\raji\blog>javac flowercls.java

C:\raji\blog>java flowercls

Flower Name: Senkanthal

Color: Red with Orange

This is the basic concept of OOPs is implemented in java.

No comments:

Post a Comment