Page 397 - CITS - Computer Software Application -TT
P. 397

COMPUTER SOFTWARE APPLICATION - CITS



           LESSON 109 - 115 : Abstract Classes and Interfaces in JAVA



            Concept of Virtual methods


              Virtual Function in Java
              A virtual function or virtual  method  in  an OOP language is a function  or method  used  to override  the
              behavior of the function in an inherited class with the same signature to achieve the polymorphism.

              When the programmers switch the technology from C++ to Java, they think about where is the virtual
              function in Java. In C++, the virtual function is defined using the virtual keyword, but in Java, it is achieved
              using different techniques. See Virtual function in C++.

              Java is an object-oriented  programming  language;  it supports OOPs  features such as polymorphism,
              abstraction, inheritance, etc. These concepts are based on objects, classes, and member functions.
              How to use Virtual function in Java

              The virtual keyword is not used in Java to define the virtual function; instead, the virtual functions and
              methods are achieved using the following techniques:
              •  We can override  the virtual function  with the inheriting class function using  the same function  name.
                 Generally, the virtual function is defined in the parent class and override it in the inherited class.
              •  The virtual function is supposed to be defined in the derived class. We can call it by referring to the derived
                 class’s object using the reference or pointer of the base class.
              •  A virtual function should have the same name and parameters in the base and derived class.
              •  For the virtual function, an IS-A relationship is necessary, which is used to define the class hierarchy in inheritance.
              •  The Virtual function cannot be private, as the private functions cannot be overridden.
              •  A virtual function or method also cannot be final, as the final methods also cannot be overridden.

              •  Static functions are also cannot be overridden; so, a virtual function should not be static.
              Examples:-
              Parent.Java:
              1  class Parent {
              2  void v1() //Declaring function

              3  {
              4  System.out.println(“Inside the Parent Class”);
              5  }
              6  }
              Child.java:

              1  public class Child extends Parent{
              2  void v1() // Overriding function from the Parent class
              3  {
              4  System.out.println(“Inside the Child Class”);

              5   }
              6    public static void main(String args[]){
              7  Parent ob1 = new Child(); //Refering the child class object using the parent class
              8   ob1.v1();
              9   }



                                                           384
   392   393   394   395   396   397   398   399   400   401   402