function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vikram Singh 157Vikram Singh 157 

Hi ! want to check the concept of Overriding

Public virtual class  Parent9 {
    public  void M1(){
   system.debug('hello parent');
    }
}
 Public class Child9 extends Parent9(){
        public override void M1(){
        system.debug('hello child'); 
            }
 }
Jyothy KiranJyothy Kiran
Hi Vikram,

The following is an extended example of a class, showing all the features of Apex classes. The keywords and concepts introduced in the example are explained in more detail throughout this chapter.
001// Top-level (outer) class must be public or global (usually public unless they contain
002// a Web Service, then they must be global)
003public class OuterClass {
004 
005  // Static final variable (constant) – outer class level only
006  private static final Integer MY_INT;
007 
008  // Non-final static variable - use this to communicate state across triggers
009  // within a single request)
010  public static String sharedState;
011   
012  // Static method - outer class level only
013  public static Integer getInt() { return MY_INT; }
014 
015  // Static initialization (can be included where the variable is defined)
016  static {
017    MY_INT = 2;
018  }
019 
020  // Member variable for outer class
021  private final String m;
022 
023  // Instance initialization block - can be done where the variable is declared,
024  // or in a constructor
025  {
026    m = 'a'; 
027  }
028   
029  // Because no constructor is explicitly defined in this outer class, an implicit,
030  // no-argument, public constructor exists
031 
032  // Inner interface
033  public virtual interface MyInterface {
034 
035    // No access modifier is necessary for interface methods - these are always
036    // public or global depending on the interface visibility
037    void myMethod();
038  }
039 
040  // Interface extension
041  interface MySecondInterface extends MyInterface {
042    Integer method2(Integer i);
043  }
044 
045  // Inner class - because it is virtual it can be extended.
046  // This class implements an interface that, in turn, extends another interface.
047  // Consequently the class must implement all methods.
048  public virtual class InnerClass implements MySecondInterface {
049 
050    // Inner member variables
051    private final String s;
052    private final String s2;
053 
054    // Inner instance initialization block (this code could be located above)
055    {
056       this.s = 'x';
057    }
058 
059    // Inline initialization (happens after the block above executes)
060    private final Integer i = s.length();
061  
062    // Explicit no argument constructor
063    InnerClass() {
064       // This invokes another constructor that is defined later
065       this('none');
066    }
067 
068    // Constructor that assigns a final variable value
069    public InnerClass(String s2) {
070      this.s2 = s2;
071    }
072 
073    // Instance method that implements a method from MyInterface.
074    // Because it is declared virtual it can be overridden by a subclass.
075    public virtual void myMethod() { /* does nothing */ }
076 
077    // Implementation of the second interface method above.
078    // This method references member variables (with and without the "this" prefix)
079    public Integer method2(Integer i) { return this.i + s.length(); }
080  }
081 
082  // Abstract class (that subclasses the class above). No constructor is needed since
083  // parent class has a no-argument constructor
084  public abstract class AbstractChildClass extends InnerClass {
085 
086    // Override the parent class method with this signature.
087    // Must use the override keyword
088    public override void myMethod() { /* do something else */ }
089 
090    // Same name as parent class method, but different signature.
091    // This is a different method (displaying polymorphism) so it does not need
092    // to use the override keyword
093    protected void method2() {}
094 
095    // Abstract method - subclasses of this class must implement this method
096    abstract Integer abstractMethod();
097  }
098 
099  // Complete the abstract class by implementing its abstract method
100  public class ConcreteChildClass extends AbstractChildClass {
101    // Here we expand the visibility of the parent method - note that visibility
102    // cannot be restricted by a sub-class
103    public override Integer abstractMethod() { return 5; }
104  }
105 
106  // A second sub-class of the original InnerClass
107  public class AnotherChildClass extends InnerClass {
108    AnotherChildClass(String s) {
109      // Explicitly invoke a different super constructor than one with no arguments
110      super(s);
111    }
112  }
113   
114  // Exception inner class
115  public virtual class MyException extends Exception {
116    // Exception class member variable
117    public Double d;
118 
119    // Exception class constructor   
120    MyException(Double d) {
121      this.d = d;
122    }
123     
124    // Exception class method, marked as protected
125    protected void doIt() {}
126  }
127   
128  // Exception classes can be abstract and implement interfaces
129  public abstract class MySecondException extends Exception implements MyInterface {
130  }
131}
This code example illustrates:
A top-level class definition (also called an outer class)
Static variables and static methods in the top-level class, as well as static initialization code blocks
Member variables and methods for the top-level class
Classes with no user-defined constructor — these have an implicit, no-argument constructor
An interface definition in the top-level class
An interface that extends another interface
Inner class definitions (one level deep) within a top-level class
A class that implements an interface (and, therefore, its associated sub-interface) by implementing public versions of the method signatures
An inner class constructor definition and invocation
An inner class member variable and a reference to it using the this keyword (with no arguments)
An inner class constructor that uses the this keyword (with arguments) to invoke a different constructor
Initialization code outside of constructors — both where variables are defined, as well as with anonymous blocks in curly braces ({}). Note that these execute with every construction in the order they appear in the file, as with Java.
Class extension and an abstract class
Methods that override base class methods (which must be declared virtual)
The override keyword for methods that override subclass methods
Abstract methods and their implementation by concrete sub-classes
The protected access modifier
Exceptions as first class objects with members, methods, and constructors
This example shows how the class above can be called by other Apex code:
01// Construct an instance of an inner concrete class, with a user-defined constructor
02OuterClass.InnerClass ic = new OuterClass.InnerClass('x');
03 
04// Call user-defined methods in the class
05System.assertEquals(2, ic.method2(1));
06 
07// Define a variable with an interface data type, and assign it a value that is of
08// a type that implements that interface
09OuterClass.MyInterface mi = ic;
10 
11// Use instanceof and casting as usual
12OuterClass.InnerClass ic2 = mi instanceof OuterClass.InnerClass ?
13                            (OuterClass.InnerClass)mi : null;
14System.assert(ic2 != null);
15 
16// Construct the outer type
17OuterClass o = new OuterClass();
18System.assertEquals(2, OuterClass.getInt());
19 
20// Construct instances of abstract class children
21System.assertEquals(5, new OuterClass.ConcreteChildClass().abstractMethod());
22 
23// Illegal - cannot construct an abstract class
24// new OuterClass.AbstractChildClass();
25 
26// Illegal – cannot access a static method through an instance
27// o.getInt();
28 
29// Illegal - cannot call protected method externally
30// new OuterClass.ConcreteChildClass().method2();
This code example illustrates:
Construction of the outer class
Construction of an inner class and the declaration of an inner interface type
A variable declared as an interface type can be assigned an instance of a class that implements that interface
Casting an interface variable to be a class type that implements that interface (after verifying this using the instanceofoperator)
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,

Overriding:
  • It is an object-oriented programming that enables the child class to provide a different implementation for a method that is already implemented in its parent class.
  • This is possible through only inheritance
  • Multiple methods containing the same name, the same signature is inherited (virtual) and another is originated (override) in the child class
Q.Why overriding?
If a parent class method serves the purpose of a child class, do not override (or) else to make the child over the ride. Hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar