Java OOPs Concepts

Java OOPs Concepts    

     Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (attributes or properties), and code in the form of procedures (methods or functions). Java is a popular programming language that fully supports object-oriented principles. Here are some key concepts of object-oriented programming in Java:

  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation 

what the an Object and Class ?

 



    Classes and Objects: In Java, everything is encapsulated within classes. A class is a blueprint or template for creating objects. Objects are instances of classes.

    In object-oriented programming (OOP), an object is a fundamental unit of a program. An object represents a real-world entity, such as a person, a car, a bank account, etc., or an abstract concept, such as a data structure, a network connection, etc.

Objects in OOP have two main characteristics:

  1. State: Also known as attributes, properties, or fields, the state of an object represents the data it holds. This data defines the object's current condition or properties. For example, for a "Car" object, the state may include attributes such as the car's make, model, color, and current speed.

  2. Behavior: Also known as methods or functions, the behavior of an object represents the actions or operations that it can perform. These actions may involve manipulating the object's state or interacting with other objects. For example, for a "Car" object, behavior may include methods such as "startEngine()", "accelerate()", "brake()", etc.

 Example code:-

//create a Object and Class

class Object

{

    int a = 10;

    // main method

    public static void main ( String args[] )

    {

        // print the object a

        // installation the constructor why

        // outside of object value create a new object code it's present

        // Syntax

        // class_name new_name = new class_name();

        Object cc = new Object ( ) ;

        // print

        System.out.println(cc.a);

        // another object print

        System.out.println("A="+cc.a);


    }

}

output : 01

 


Note:-

 

i create two object its 

1. 10

2. 10

i create class also two

1.  class object

        {     

            int a;

        }

2. class object 

        {

            main method

            // but an object present any block of variables to call 

                different method in the main body or main method    

                block

        }

    

 Inheritance

  Inheritance is a mechanism in which one class acquires the properties and behaviors of another class. The class that is inherited from is called the superclass or base class, and the class that inherits is called the subclass or derived class.

Syntax:-

class a{

print("A");

}

class b{

print("B")

}

class c{

    main method

    {

        //call constructor

        a cc1 = new a ( ) ;

        b cc2 = new b ( ) ;

        print(cc1.a);

        print(cc2.b);

    }

}

Example code : 02

//parent class declare
class parent
{
    int a = 10 ;
}
//child class declare
class child extends parent
{
    int b = 20 ;
}
//Inheritance class
class inheritance extends child
{
    //main method
    public static void main(String[] args)
    {
        // call constructor
        inheritance cc = new inheritance();

        int c;
        c= cc.a + cc .b;
        System.out.println("C="+c);
    }
}

ouput:02


Polymorphism:

        Polymorphism means the ability to take many forms. In Java, polymorphism allows objects of different classes to be treated as objects of a common superclass through inheritance. It can be achieved through method overriding or method overloading.

syntax:-

 


        

     

         

 

 

 

Comments

Popular posts from this blog

digital marketing ppt-u1

SOFTWARE

cn lab

Computer Operations and Performing - D L Unit-1-1

DBMS Degree Lab Records

DS-Record-mca-04) write a program for evaluating a given postfix expression using stack.

Java unit-1 history , C++ vs Java , java environment

Access the Internet to Browse Infromation & E-Mail Operation- D L Unit-2-1

OAT-Record-01) Visiting card

java program structure,java Tokens, keywords,