Tuesday, August 1, 2023

Java Object Oriented Programming

 

Introduction to Object Oriented Programming

To manage increasing complexity , the second approach called Object Oriented Programming

Abstraction - Hiding internal details and showing functionality

Encapsulation - Binding and wrapping the data into single unit

Inheritance - One object acqires properties and behaviour of parent class

Polymorphism - One interface Multiple Method

Class

·      Collection of Object

Object

·      Collection of data

Class Fundamentals

·      Class is a template which contains information about an object.

·      Class defines a new data type

·      An object is an instance of a class which hold the real data

·      User can create any numbers of objects from a single class.

Syntax

class classname{

type instance_variable1;

type instance_variable2;

// …

type instance_variable2;

 

type methodname1(parameter-list){

     //body of method

}

type methodname2(parameter-list){

    //body of method

}

    //…

type methodnameN(parameter-list){

   // body of method

  }

}

A Simple Class

class Box{

       double width;

       double height;

      double depth;

}

Example 1

File name: Box.java

class Box {

       double width;

       double height;

       double depth;

}

//This class declares an object of type Box.

File name: BoxDemo.java

class BoxDemo{

   public static void main(String args[]){

       Box mybox = new Box();

       double vol;

       //assign values to mybox's instance variable

     mybox.width = 10;

     mybox.height = 20;

     mybox.depth = 15;

     //computer volumn of box

    vol=mybox.width * mybox.height * mybox.depth;

    System.out.println("Volumn is"+vol);

}

}

Output



Example 2

File name: Box.java

class Box {

       double width;

       double height;

       double depth;

//This class declares an object of type Box.

File name: BoxDemo1.java

class BoxDemo1{

   public static void main(String args[]){

       Box mybox1 = new Box();

       Box mybox2 = new Box();

       double vol;

       //assign values to mybox's instance variable

     mybox1.width = 10;

     mybox1.height = 20;

     mybox1.depth = 15;

     mybox2.width = 3;

     mybox2.height = 6;

     mybox2.depth = 9;

     //compute volume  of  1st box

    vol=mybox1.width * mybox1.height * mybox1.depth;

    System.out.println("Volumn is"+vol);

    //compute volume  of  2nd box

    vol=mybox2.width * mybox2.height * mybox2.depth;

    System.out.println("Volume  is"+vol);

}

}

Output



Declaring Objects

Box mybox;  // declare reference to object

mybox=newBox();  //allocate a box object

 


A Closer Look at new

class-var=new classname();

this situation is depicted here:

b1                           width

                                height           box object

b2                            depth    

 

Example:

//object reference example

File name: ObjDemo.javaclass 

class ObjDemo

{

  int a;

public static void main(String args[])

{

          ObjDemo o1,o2;

           o1=new ObjDemo();

           o1.a=100;

           o2=new ObjDemo();

           o2.a=100;

   if(o1==o2)

    System.out.println("Both are equal");

     else

       System.out.println("Both are not equal");

     //reference of object o1 is assigned to object o2

o2=o1;

if(o1==o2)

        System.out.println("Both are equal");  

else

          System.out.println("Both are not equal");

  }

}

Output



0 comments:

Post a Comment