Introduction of OOPS CONCEPT in JAVA

  • By
  • November 1, 2021
  • JAVA Programming

Introduction of OOPS CONCEPT in JAVA –

1.Inheritance –

Inheritance is defined as it is the capability of one class to inherit the properties or attributes of another class.  Inheritance is implemented in java to achieve reusability.

Sub class: The class which inherits the properties of another class is called as child class/derived class/sub class/new class.

Super class: The class which is inherited/accessible within another class is called as parent class/base class/ super class /old class.

extends: Extends keyword indicates that the properties of base class are extended/inherited in derived class.

 Types of inheritance –

1. Single level inheritance –

When one class inherits another class it is called single level inheritance.

Single level inheritance consist of only one base class and one derived class

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Program

package inheriatnceEx;

class ElectronicDevice  //base class ElectronicDevice

{

         String name;

         String c_name;

         int price;

         String color;

        

         void repair() //instance method

         {

                     System.out.println(“In repair()..”);

         }

}

 

 

//child class Ac inherits the properties of base class //ElectronicDevice

 

class  Ac extends ElectronicDevice

{

         String warranty;

        

         void readData(String name,String c_name,int   price,String color//instance method to initialized insatnce varibles of both parent class and child class

        

  {

           this.name=name;

           this.c_name=c_name;

    this.price=price;

    this.color=color;

         }

         void show()

         {

                     System.out.println(“name of device is : “+name);

                     System.out.println(“company name of device is : “+c_name);

                     System.out.println(“price of device is : “+price);

                     System.out.println(“color of device is : “+color);

         }

}

 public class SingleLevelDemo

{

  public static void main(String[] args)

  {

               Ac a1=new Ac();

                    

                     a1. repair();

                     System.out.println();

                     a1.readData(“ac”,“samsung”,30000,“white”);

                     a1.show();

         }

 

}

/*output

In repair()..

 name of animal is : ac

company name of device is : samsung

price of device is : 30000

color of device is : white */

2. Multilevel Inheritance –

In multilevel inheritance, one derived class is derived from another derived class and that derived class is derived from one base class.

Java Training In Pune can implement the same above program through multilevel inheritance.

package inheriatnceEx;

 

class ElectronicDevice  //base class ElectronicDevice

{

         String name;

         String c_name;

         int price;

         String color;

        

         void repair()

         {

                     System.out.println(“In repair()….”);

         }

}

class  Ac extends ElectronicDevice //child class Ac inherits the properties of base class ElectronicDevice

{

         String warranty;

        

         void purchase()

         {

                     System.out.println(“In purchase()….”);

         }

}

 

//child most class DuctlessMiniSplit inherits the properties of its parent class Ac

class DuctlessMiniSplit extends Ac  

{

         String state;

        

         void readData(String name,String c_name,int price,String color,String state//instance method to initialized insatnce varibles of both parent class and child class

         {

           this.name=name;

           this.c_name=c_name;

      this.price=price;

        this.color=color;

        this.state=state;

         }

         void show()

         {

                     System.out.println(“name of animal is : “+name);

                     System.out.println(“company name of device is : “+c_name);

                     System.out.println(“price of device is : “+price);

                     System.out.println(“color of device is : “+color);

                 System.out.println(“the state of device is  : “+state);

         }

}

public class MultiLevelDemo

{

  public static void main(String[] args)

  {

           DuctlessMiniSplit d1=new DuctlessMiniSplit();

                           d1.repair();

                     System.out.println();

                    

                     d1.purchase();

                     System.out.println();

                             d1.readData(“ac”,“samsung”,30000,“white”,“on”);

                     d1.show();

         }

}

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

3. Hierarchical Inheritance –

In this, we have one base class and multiple derived classes   OR

In hierarchical inheritance multiple derived classes are derived from a single base class.

package inheriatnceEx;

class ElectronicDevice  //base class ElectronicDevice

{

         String name;

         String c_name;

         int price;

         void repair()

         {

                     System.out.println(“In repair()….”);

         }

}

class  Ac extends ElectronicDevice //child class Ac inherits the properties of base class ElectronicDevice

{

         String warranty;

         void derivedData1(String name,String c_name,int price,String warranty//instance method to initialized insatnce varibles of both parent class and child class

         {

           this.name=name;

           this.c_name=c_name;

            this.price=price;

             this.warranty=warranty;

         }

        

         void derivedDisplay1()

         {

                     System.out.println(“name of animal is : “+name);

                     System.out.println(“company name of device is : “+c_name);

                     System.out.println(“price of device is : “+price);

                    

                     System.out.println(“the warranty of device is  : “+warranty);

         }

}

class Tv extends ElectronicDevice //child class Ac inherits the properties of base class ElectronicDevice

{

         String state;

        void derivedData2(String name,String c_name,int price,String state//instance method to initialized insatnce varibles of both parent class and child class

         {

           this.name=name;       //child class Tv accessing data of base class ElectronicDevice

           this.c_name=c_name;

      this.price=price;

  

    this.state=state; //data of its own class

         }

          void derivedDisplay2()

         {

                     System.out.println(“name of animal is : “+name);

                     System.out.println(“company name of device is : “+c_name);

                     System.out.println(“price of device is : “+price);

                    

                     System.out.println(“the state of device is  : “+state);

         }

        

}

public class HierarchicalDemo

{

  public static void main(String[] args)

  {

                     //In case of hierarchical inheritance, Java Classes In Pune need to create the object of each and every derived class. Here,we have 2 derived class so 2 objects will be created

    Ac a1 =new Ac();

                    

                     a1.repair();

                     System.out.println();

                            a1.derivedData1(“AC”, “LG”, 56000, “2 years”);

                     a1.derivedDisplay1();

                     System.out.println();

                    

                    

                  Tv t1 =new Tv();

                             t1.repair();

                     System.out.println();

                    

                     t1.derivedData2(“Tv”, “Sony”, 60000,“on”);

                     System.out.println();

                     t1.derivedDisplay2();

         }

 }

/*output:

In repair()….

 

name of animal is : AC

company name of device is : LG

price of device is : 56000

the warranty of device is  : 2 years

 

In repair()….

 

name of animal is : Tv

company name of device is : Sony

price of device is : 60000

the state of device is  : on */

4. Multiple Inheritance –

In multiple  inheritance,  we have  multiple base classes and have only one derived class. In Java, we can’t achieve multiple inheritance through classes. We can achieve multiple inheritance through interface (will see it in another blog).

 1.Polymorphism – 

Polymorphism is defined as, one object as one form.  The word Polymorphism is the combination of two Greek words, poly + morphs = polymorphism

Where, poly means many and morphs means forms.

For example, one person has many forms, meaning a person can be a teacher, person can be a doctor, engineer, and children and so on.

Java Course In Pune can achieve polymorphism in java by two ways –

  1.     Compile time polymorphism
  2.     Run time polymorphism

 1.Compile time polymorphism/static binding/static polymorphism/static method dispatch process –

  • Fun call is resolved/bind/link with fun definition at compile time that’s why it is called as compile time polymorphism
  • It is known as compile time polymorphism because the decision of which method is called is made by compiler at compile time.

e.g Method overloading

If class has multiple methods having same name but different parameter lists (type and no of arguments of each class should be different) then it is called as method overloading.

There are 2 ways to overload the methods – 

  1. By changing the no of arguments
  2. By changing the data types of arguments

We can’t perform the method overloading by changing the return type of method.

Program for method overloading by changing the no of arguments

package class_objectPrograms;

 

//method overloading by changing the no of of arguments

class OverloadingDemo

{

         void sum()

         {

                     System.out.println(“method having no parameters”);

         }

         void sum(int a,int b) //method definition

         {

                    

                     System.out.println(“addition is :  “+(a+b));

         }

        

         void sum(int a,int b,int c)

         {

                     System.out.println(“addition is :  “+(a+b+c));

         }

        

         void sum(int a,int b,int c ,int d)

         {

                     System.out.println(“addition is :  “+(a+b+c+d));

         }

}

 

public class MethodOveloading {

 

         public static void main(String[] args) {

 

                  OverloadingDemo o1=new OverloadingDemo();

  o1.sum();

                             o1.sum(20,30);//method call

                             o1.sum(15,7,4);

                       o1.sum(4,3,6,10);

                     }

 

}

/* output

method having no parameters

addition is :  50

addition is :  26

addition is :  23 */

 

Program for method overloading by changing the types of arguments –

class OverloadingDemo 

{

         void sum()

         {

                System.out.println(“method having no parameters”);

         }

         void sum(int a,int b) //method definition

         {

                    System.out.println(“addition is :  “+(a+b));

         }

        

         void sum(int a,double b)

         {

                   System.out.println(“addition is :  “+(a+b));

         }

        

         void sum(double a,int b)

         {

                     System.out.println(“addition is :  “+(a+b));

         }

         void sum(double a,double b)

                     {

                                 System.out.println(“addition is :  “+(a+b));

                     }

}

 

public class MethodOveloading {

 

         public static void main(String[] args) {

             OverloadingDemo o1=new OverloadingDemo();

          o1.sum();

                            o1.sum(20,30);//method call

                            o1.sum(15,7.4);

                             o1.sum(4.3,5);

                     o1.sum(4.3,5.4);

                 }

}

/*output

method having no parameters

addition is :  50

addition is :  22.4

addition is :  9.3

addition is :  9.7 */

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

2. Dynamic Polymorphism/runtime polymorphism / dynamic binding/dynamic dispatch method – 

  • Fun call is resolved/bind with fun definition at run time that’s why it is called as run time polymorphism

e.g method overriding

  • If a sub class has methods having the same name & same parameter lists as a method in the super class then it is called method overriding.
  • It means redefining the method of super class into its child class
  • We can’t override private and final method in runtime polymorphism

Program for method overriding –

package class_object;

 class Vehicle1

{

         String fuel()

         {

                     return “petrol”;

         }

         int speed()

         {

return 60;

         }

}

class Car1 extends Vehicle1 

{

         String fuel()                               //speed() and fuel() method of parent/base/super class i.e Vehicle1 here is override in Car1 class

         {

                     return “CNG”;

         }

        int speed()

         {

                     return 80;

         }

}

class Bus1 extends Vehicle1

{

         int speed()

         {

                     return 50;

         }

}

 //not a  single method of base class gets  override in Bike1 class so, it inherits the properties of base class

class Bike1 extends Vehicle1

{

        

        

}

 

class Truck1 extends Vehicle1

{

         String fuel()

         {

                     return “diesel”;

         }

  }

public class OverridingDemo1 {

    public static void main(String[] args) {

                     Car1 c1=new Car1();

                     System.out.println(“car fuel is : “+c1.fuel());

                     System.out.println(“car speed is : “+c1.speed());

                     System.out.println();

                    

                     Bus1 b1=new Bus1();

                     System.out.println(“bus fuel is : “+b1.fuel());

                     System.out.println(“bus speed is : “+b1.speed());

                     System.out.println();

                    

                     Bike1 bb=new Bike1();

                     System.out.println(“bike fuel is : “+bb.fuel());

                     System.out.println(“bike speed is : “+bb.speed());

                     System.out.println();

                    

                     Truck1 t1=new Truck1();

                     System.out.println(“truck fuel is : “+t1.fuel());

                     System.out.println(“truck speed is : “+t1.speed());

         }

}

 

/*

output:

car fuel is : CNG

car speed is : 80

 

bus fuel is : petrol

bus speed is : 50

 

bike fuel is : petrol

bike speed is : 60

 

truck fuel is : diesel

truck speed is : 60  */

 

Author:-

Pooja Bhavsar 
Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

 

 

 

 

 

 

Submit Comment

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

*
*