Tuesday, July 30, 2024

OOPs: Object-Oriented Programming

 

OOPs: Object-Oriented Programming

Examples:

  • Java
  • .Net
  • Python
  • C++
  • Ruby

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design and develop application

If any Programming language, which supports OOPs features like

OOPS Features:

1. Class and Object

2. Inheritance

3. Polymorphism

4. Abstraction

5. Encapsulation

1. Class and Object :

Without Defining class , we cannot write any simple java program

without writing class ,    ""                ""

Define class :

class  is predefined k/w in java to define class / write class

Syntax:

class  someName

{

}

class A

{

  public static void main (String args [] )

{

}

}

Class: A blueprint or template for creating objects

Class is blue print (or) pre-defined template, which describes Object characteristics + functionality of an object

Technically class contains instance variables +  methods.

Ex: Blueprint for a House:

A house needs a detailed plan in a chart, which includes various sections and their sizes. Below is an example blueprint for a house:

House Blueprint

1. Hall:

  • Size: 12 feet x 6 feet
  • Purpose: Living area for family gatherings, entertainment, and relaxation.

2. Bedroom:

  • Size: Specify the size based on your requirement (e.g., 10 feet x 12 feet)
  • Purpose: Private room for sleeping and personal activities.

3. Store Water:

  • Size: Specify the size based on your requirement (e.g., 4 feet x 4 feet)
  • Purpose: Storage area for water tanks and other related utilities.

4. Bathrooms:

  • Size: Specify the size based on your requirement (e.g., 6 feet x 8 feet)
  • Purpose: Area for bathing, showering, and other personal hygiene activities.

 

blue print :     house --> need some plan  in chart

       hall                -->  size 12*6

       bedroom  -->

       store water --> 

       bathrooms -->

 

Without defining class, we cannot create object.

If we want to create object, we must create class.

Object :

An object is an instance of a class.

It is anything that is visible and has some properties (or characteristics or attributes) along with behaviors (or functionalities).

                                                                                                 +  

ex 1: Dog

Properties of the Object:

  • Color: black/white
  • Weight: 20 grams
  • Breed: some breed

Behavior/Functionality of the Object:

  • barking()
  • jump()
  • run()
  • identifyPerson()

Example 2: Pen

Characteristics of Pen:

  • Name: Reynold, Cello
  • Shape:
  • Height:
  • Width:
  • Color: black/red
  • Nib size: 0.2mm

 

Behavior/Functionality of Pen:

  • writingOnPaper()
  • writingOnPlasticPaper()
  • writingOnDabba()
  • changeRiffle()
  • changeInk()

Summary

  • Object: An instance of a class with properties and behaviors.
  • Properties: Attributes that describe the object (e.g., name, shape, height, width, color, nib size).
  • Behaviors: Functions or methods that the object can perform (e.g., writingOnPaper, writingOnPlasticPaper, writingOnDabba, changeRiffle, changeInk).

Example 3: Student

Properties of Student Object:

  • name: Ramu
  • color: black/white
  • height: 5.5 ft
  • gender: M or F (char data type)

Behavior/Functionality of Student Object:

  • playCricket()
  • run()
  • jump()
  • getMarks()

In programming, object properties are defined in the form of variables:

·  name: Ramu → String name;

·  color: black/white → String color;

·  height: 5.5 ft → double height;

·  gender: M or F → char gender;   

 

ex4 :  HW   employee

   Properties of object:

------------------------

   behaviour of Object:

--------------------

Behavior or functionality of an object is usually described in the form of methods in programming languages:

 M1()

{

}

barking()

{

System.out.println("bow - bow");

}

IdentifyPersons()

{

System.out.println("Identify Persons");

}

playCricket()

{

}

Run()

{

Object Definition

·       Object is an instance of a class or a copy of a class (copy of variables + copy of methods).

·        To create an object, you must first define or write a class.

·        Without defining a class, you cannot create an object.

 A class describes the characteristics and behavior of an object:

  • Characteristics (Attributes): Represented as variables.
  • Behavior (Functionality): Represented as methods.

A class describes the properties of object and functionality of an object:

So, a class contains both the properties (variables) and the behaviour (methods) of an object.

                   variables   +    Methods

class  A

{

 

Variables

Methods

}

 

 

Example: Defining a Class with Variables and Methods

class   Dog

{

    // variables =

  String name ;

  String color;    

 float weight;

              

  // Methods

  barking()

{

 System.out.println("bow - bow");

}

}

 Create object :

   first we should write class

Syntax:

className   objName (or) ObjRefvariable =  new className();

className: The name of the class (e.g., A, Dog, Employee).

objName (or objRefVariable): A meaningful variable name that references the object.

new: A predefined keyword in Java used to create an object for the given class name.

 

package OOPSBasics;

public class Dog {

               //  properties

               // Declare variables name, color, weight

               String name;

               String color;

               float weight;

               //  behaviour of dog- Define method- barking

               public void barking()

               {

                              System.out.println("bow - bow");

               }

               public static void main(String[] args)

               {

                              //  create obj  for 'dog' class

                              //                                          Syntax:     ClassName  objRefName =  new ClassName();

                              Dog d1 = new Dog();

                              // new predefined k.w in java

                              // which can be used to create obj  for the given class i.e Dog class

               }

}

Creating an Object for the Student Class:                                                                         

package OOPSBasics;

public class Student {

               // Declare variables - name, address, gender

               String name;

               String address;

               char gender;

               // Define playCricket()

               public void playCricket()

               {

                              System.out.println("plays cricket");

               }

               public static void main(String[] args) {

                              //   create obj for 'student' class

                              //                                          synatx: classname  objref=  new className();

                              Student s1 = new Student();

               }

}

HW  Create Employee class  and create obj

HW  Create Pen  class and Create obj

Creating object inside Same class:

Access Variables and Methods Using Object name or object reference variable:

package OOPSBasics;

public class Dog {

               //  properties

               // Declare variables name, color, weight

               String name;

               String color;

               float weight;

               //  behaviour of dog- Define method- barking

               public void barking()

               {

                              System.out.println("bow - bow");

               }

               public static void main(String[] args)

               {

                              //  create obj  for dog class

                              //                                          Synatx:     ClassName  objRefName =  new ClassName();

                              Dog d1 = new Dog();

                             

                              // new predefined k.w in java

                              // which can be used to create obj  for the given class i.e Dog class

                              // access variables  and store "Dolly" in name, color = "white";weight = 20.5f;

                                                            //                                          syntax:  refVar.VariableName

                                             d1.name = "Dolly";

                                             d1.color ="white";

                                             d1.weight = 20.5f;

                                                            // display  name

                                                            // display color                                                                                                                                                                                                                                                                                                                

                                                            // display weight

                                             System.out.println("d1 name="+ d1.name);

                                             System.out.println("d1 color ="+ d1.color);

                                                            System.out.println("d1 weght ="+ d1.weight);

                                                            //                                 20.5                                                                                           

                                                            //  call methods using refVar

                                                            //                                          synatx:   referVar.MethodName();

                                                            //  call barking();              

                                                                           d1.barking();

               }

}

o/p:

d1 name=Dolly

d1 color =white

d1 weght =20.5

bow - bow

 

ex2:

                                                            package OOPSBasics;

                                                           

                                                            public class Student {

                                                                           // Declare variables - name, address, gender

                                                                           String name;

                                                                           String address;

                                                                           char gender;

                                                           

                                                                           // Define playCricket()

                                                                           public void playCricket()

                                                                           {

                                                                                          System.out.println("plays cricket");

                                                                           }

                                                                           public static void main(String[] args) {

                                                                                          //   create obj for 'student' class

                                                                                          //                                          synatx: classname  objref=  new className();

                                                                                          Student s1 = new Student();

                                                           

                                                                                          //  store name = "Ram";gender = 'M'; Address  =  "BNG";

                                                                                                         s1.name = "Ram";

                                                                                                         s1.gender = 'M';

                                                                                                         s1.address = "BNG";

                                                           

                                                                                          //  display s1 name  and s1 gender

                                                                                                         System.out.println("s1 name="+ s1.name);

                                                                                                         System.out.println("s1 gender="+ s1.gender);

                                                           

                                                           

                                                                                          //  call PlayCricket()        

                                                                                                         s1.playCricket();

                                                           

                                                                           }

                                                           

                                                            }

o/p:

s1 name=Ram

s1 gender=M

plays cricket

Creating object inside other class:

package OOPSBasics;

public class Dog

{             

               //    variables ( properties)

               // Declare variables name, color, weight

               String name;

               String color;

               float weight;

               //  behaviour of dog--->  Define method- barking              

               public void barking()

               {

                              System.out.println("bow - bow");

               }

}

----------------------

package OOPSBasics;

public class TestDog {

               public static void main(String[] args) {

                              // create obj for 'Dog' class

                              Dog d1 = new Dog();

                              // name = "Dolly";

                              d1.name  = "Dolly";

                              //   display d1 name

                              System.out.println("d1 name="+ d1.name);

                              //  call Methods of Dog -class -barking();

                              d1.barking();

                              // note :If we want to access variable and Methods of 'Dog'Class inside other classes,

                              //first we must create obj and by using reference variable name or By using object name,

                              //we can access all instance variables

                              //  and also instance methods of Class

               }

}

Create Multiple objects for the same class:

package OOPSBasics;

public class TestDog {

               public static void main(String[] args) {

                              // create obje for 'Dog' class with ref variable d3  and call barking method

                              Dog d3 = new Dog();

                              d3.name = "D3";

                              d3.weight = 3.5f;                            

                              d3.barking();

                              //  create multiple objects for Dog class inside the same class or other classes

                              // create obje for Dog class with ref variable d4  and name  =" D4 ";weight = 4.5f;

                              Dog d4 = new Dog();

                              d4.name = "D4";

                              d4.weight = 4.5f;                            

                              // call barking method

                              d4.barking();

                              //// create obje for Dog class with ref variable d5  and name  =" D5 ";weight = 5.2f;

                              Dog d5 = new Dog();

                              d5.name = "D5";

                              d5.weight = 5.2f;

                             

                              // // call barking method

                              d5.barking();

               }

}

-----------------------------

package OOPSBasics;

public class TestStudent {

               public static void main(String[] args) {

                              // TODO Auto-generated method stub

                              // creating object For 'Student'class with ref variable s1 and name = "Ramu",gender = 'M';

                              //Address = "Bng";

                              //                           ex:  classname

                              Student s1 = new Student();

                              s1.name = "Ramu";

                              s1.gender = 'M';

                              //                           address = "BNG";/ error  as we did not use refer variable name i.e s1.address

                              s1.address = "Bng";

                              // display s1 name

                              System.out.println("s1 name="+ s1.name);

                              //                             Ramu

                              // call playCricket();       

                              //                           playCricket();// error  as we did not use refer variable name i.e s1.playCricket();

                              s1.playCricket();

                              // create obj of student class  with ref variable s1

                              //                           can we create duplicate ref variable s1 ?

                              //                           Student  s1 = new Student();//  Error :  Duplicate variable name s1 is not allowed

                              //                           int a;

                              //                           int a;//  error

                              // create obj of student class  with ref variable s2 and

                              //name = "Sita";gender = 'F';Address = "Chennai";

                              Student s2 = new Student();

                              s2.name= "Sita";

                              s2.gender = 'F';

                              s2.address = "Chennai";

                              // display s2 name

                              System.out.println("s2 name="+ s2.name);

                              //  call playCricket();

                              s2.playCricket();

                              //we can create obj for any class,   Dog, employee..etc

               }

}

o/p:

-----

s1 name=Ramu

plays cricket

s2 name=Sita

plays cricket

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...