Java OOPS (Object Oriented Programming ) Concepts

 

 

Object-Oriented  Programming (OOP) in  Java  is  an  important  concept  which  includes  core  principles  of  Java  such as  inheritance , encapsulation ,  Polymorphism , Abstraction  etc   along  with  Classes  &  Objects  which  provides  a  structured  approach  to  build  a  reusable  and  maintainable  software  application.

 

 

Before  Object-oriented  Programming ( OOPS) ,  Procedural approach  was  used  to  build a  software  application  where  programmer  used  to  write   step-by-step  functions  which  made  it  harder  to  manage  and  reuse  code  in  the  large  applications.

 

To  overcome  these  limitations , Object-oriented  Programming  was  introduced .  

 

 

  

 

 

 

Key  Featnures  of  OOP  in  Java:

 

 

  • OOPS  concept  keeps  related  data  and  methods  together  which  is  known  as  Encapsulation
  • OOPS  in  Java  structures  the  code   into  logical  units  using  classes  and  objects
  • OOPS  concept  make  the  Java  code  modular ,  reusable  and  scalable
  • Java  OOPS  prevents  unauthorized  access  to  data
  • OOPs  concept  provides  very  important  concepts  such  inheritance , encapsulation , polymorphism , abstraction  etc .

 

 

 

 

 

Characteristics   of  an  OOP  (Object  Oriented  Programming )

 

JAVA OOPS CHARACTERISTICS
JAVA OOPS CHARACTERISTICS

 

  1. Class

         A  class  in  Java   is  a  user-defined   prototype  or  blueprint  from  which  objects  are  created .   Java  class  can  contains  fields , methods ,constructors , nested classes , interfaces , functions  etc. Class  represents  the  set  of  properties   or  methods  that  are  common  to  all  objects  of  one  type .  Using  Java  class ,  one  can  create  multiple  objects  with  the  same  behavior  instead  of  writing  their  code multiple  times . 

 

Class  declaration  include  following  components :

  • Modifiers : A  class  can  be  public  or  can  have  default  access  modifier
  • Class  Name : Java  class  name  should  begin  with  the  initial  letter  capitalized  by  naming  convention.
  • Body :  The Java  class  body is  surrounded  by  braces { } .

 

 

 

Basic  Java  Class  Syntax :

 

[Access_Modifier ]  class   ClassName   {

// Class  body: fields , methods , constructors  etc .

}

 

 

 

 

Example :

 

public  class   World{

  

      public  static  void  main (String  args [] ) {

                 System.out.println(” This  world  is  Beautiful “);

}

}

 

 

  

 

2 . Object :

 

In  Java ,  object  is  an  instance  of  a  class .  An  object  is  a  basic  unit  of   Object -Oriented  Programming  that  represents  real-life  entities .  Object  is  a  physical  entity, whereas  class  is  a  logical  entity .

 

An  object  mainly  consist  of :

 

  • State :   State  of  an  object  represents  the  data  or  properties  of   an  object, which  are  stored  in  fields  or  variables .  For  example ,  Dress  object  might  have  a  color ( e.g. , pink )  and category(e.g. college  uniform ). So  here , color  red  and  category  college   uniform  are  states  of  object  Dress.
  • Behavior : Behavior  represents  the  actions   an  object  can  perform .   A  Dress  objects  behavior  may  include  wear( )  or  stich ()
  • Identity :  It  is  a  unique  name  given  to  an  object  that   enables  it  to  interact  with  other  objects .
  • Method :   A  method is  a  collection  of  statements  that  perform  some  specific   task  and   return  the  result  to  the  caller .

 

 

 

 

Example :

 

public  class   Gift  {

 

        //Instance  variables

        private  int  score;

       private  String  gift ;

 

      // Constructor

        public  Gift ( int  score ,  String  gift ) {

           this. score=  score ;

          this.gift = gift ;

}

 

   // getters  method

 

   public  int  getScore () {

      return  score ;

}

 

public  String  getGift () {

     return  gift ;

}

 

   // setters  method

 

public  void  setScore ( int  score ) {

    this.score =score ;

}

 

public  void  setGift ( String  gift ) {

 this.gift =gift ;

}

 

//Instance  method

 

public  void  displayDetails () {

System.out.println(“Score is :”+score);

System.out.println(“Gift is :”+gift);

 

}

 

public  static  void  main (String  args [] ) {

 

Gift  gif =  new  Gift ( 95, “$500”);

gif.displayDetails();

}

}

  

 

 

OUTPUT :

score : 95

gift : $500

 

 

 

 

3.Encapsulation:

 

Encapsulation  in  Java  is  a  mechanism  of  wrapping  the  code  and  data  together  into  a  single  unit.

 

Key  Characteristics  of Encapsulation  in  Java:

 

  • Encapsulated  class  can  hide  the  implementation  details  and  discloses  only  the   functionality  to  the  user. By  making  class  data  and  methods  private , representation  or  implementations  can  later  be changed  without  impacting  the  class  code.
  • Encapsulation  helps  in  better  maintainability, readability  and  usability. Encapsulation  also  helps  in  data  integrity .

 

 

 

 

Implementation  of  Encapsulation  in  Java:

 

 

  • Declare  data  as  private :   By  declaring  class  data  as  private , hide  the  class  data  so  that  it  cannot  be  accessed  directly  from  outside  the  class.
  • Use  getters  and  setters :   Keep  the  variables  private  and  provide  public   getter  and  setter  methods  for  safe  modification  and  for  controlled  access  with  validation.
  • Use  Proper  Access  Modifiers :  Use   private  access  modifier  for  data  hiding  and  public  for  methods  that  provide  access.

 

 

 

 

Example :

 

 

class  Gift {

//Declare  the  variable  as  private

  private  String   giftName;

     //Getter  method  used  to  get  the  data

      public  String  getGiftname() {

return   giftName;

        }

 

// Setter  method  is  used  to  set   or  modify   the  data

 

public  void  setGiftname (String  giftName ) {

this.giftName=giftName;

  }

 

}

 

public  class  EncapsulationDemo{

 

public  static  void  main (String  args [] ) {

 

Gift   gif = new  Gift ();

gif.setGiftname(” $ 1000″ );

System.out.println(“Gift Name : “ +gif.getGiftname());

 

}

}

 

 

 

OUTPUT :

 

Gift Name : $1000

 

 

Advantages  of  Encapsulation :

 

The  advantages  of  Encapsulation  are  as  follows :

 

  • Data  Hiding :  Encapsulation   protects  sensitive  data  from   unauthorized  access  by  resticting  direct  access  to  class  variables .
  • Enhanced  Security : Encapsulation  allows  control  and  validation  over  data , preventing  invalid  or  harmful  values  from  being  set.
  • Improved  Maintainability : In  Encapsulation , changes  to  internal  implementation  can  be  made  without  affecting  external  code  that  uses  the  class
  • Code   Reusability :  Encapsulated  classes  can  be  reused  in  different  programs  without  changing  the  internal  logic.
  • Modularity:  Encapsulation   promotes  organized , modular  code  by  keeping  data  and  methods  together  within  a  class.

 

 

 

 

Disadvantages  of  Encapsulation:

 

  • Performance  Issue:  By  accessing  data  through  methods  instead  of  directly  accessing  can  introduce  a  minor  performance  cost , espicially  in  critical  application.
  • Increased  Code  Complexity :  In  encapsulation,  by  writting  getter  and  setter  methods  for  every variable  can  make the  code  more  longer  and  complex.
  • Less  Flexibility:  By  restricting  access  to  class members  may  affect  the  ability  of  other  classes  to  extend  or  use  the  encapsulated  class  efficiently.

 

 

 

 

 

4. Inheritance :

 

 

Inheritance  in  Java  is  a  fundamental  principal  in  OOP ( Object-Oriented  Programming )   by  which  one  class  is  allowed  to  inherit  the  properties (fields)  and  behaviors (methods )  of    another   class .  

 

A   class  that  inherits  the  another  class  can  reuse  the  properties  of  that  class .

 

 

 

Example :

 

 

//Parent  class

class  Gift {

     void  gifName() {

 System.out.println(“Gift  is  awesome”);

}

}

 

 

//Child  class

class  Studentgift    extends  Gift   {

    void  giftName () {

     System.out.println(“Gift  is  incredible  book “);

   }

}

 

//Child  class

 

class   Teachergift   extends  Gift  {

        void  giftName() {

   System.out.println(“Gift  is $11000”);

         }

}

 

//Main  class

 

public  class   InheritanceDemo  {

public  static  void  main (String  args[] )  {

  Gift   gif ;

   gif =  new   Studentgift();

   gif.giftName();

 

    gif =  new   Teachergift();

    gif.giftName();

 

}

 

}

 

 

 

OUTPUT:

 

Gift  is  incredible  book

Gift is  $11000

 

 

 

 

Advantages  of Inheritance  in  Java :

 

 

 

  • Reusability  of  Code :  The  code  written  in  parent  class(super class )  is  common  to  all  subclasses .  Child  classes ( Sub classes )  can  directly  use  the  parent  class  code .
  • Method  Overriding :  Method  overriding  is  achieved  by  only  inheritance .  Method  overriding  is  one  of  the  ways  by  which  Java  achieves  Run Time  Polymorphism.
  • Abstraction : Abstraction  is a  process  where  implementation  details  are hidden and  only  shows  the  functionality  to  the  user . This  concept  of  abstraction  is  achieved  through  inheritance .

 

 

 

 

 

HOW  INHERITANCE  WORKS  IN  JAVA ?

 

 

For  inheritance  concept,  extends  keyword  is  used  in  Java . This  extends   keyword    enables  the  subclass  to   inherit  the  properties  of  superclass .  When  a  class  extends   another  class  ,  it  inherits    the  properties  of  the  superclass  .  When  a  class  extends  another  class ,  it  means  it  inherits  all  the  non-primitive  members  of  the  superclass  . The  subclass  can also  override  the  methods  of  superclass  or  can  add  new  functionality  to  them .

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *