Method Overwriding

If a subclass has the method name identical to the method name defined in the parent class then it is known to be Method Overriding i.e, the sub-classes which are inherited by the declared super class, overrides the method defined in the super class utilizing the override keyword.

when a subclass wishes to impart a particular implementation for the method defined in the parent class then that subclass overrides the defined method from the parent class. When we wish to reconstruct the method defined in the super class then we can apply method overriding.

class School

   def NumberOfStudents()=
    { 
        0 // Utilized for returning an Integer
    } 

 
class class_1 extends School
{

    override def NumberOfStudents()=
    { 
        30
    } 

 
class class_2 extends School

    override def NumberOfStudents()=
    { 
        32
    } 


class class_3 extends School

    override def NumberOfStudents()=
    { 
        29
    } 


object Demo1 {
      
  def main(args : Array[String]){
        var x=new class_1() 
        var y=new class_2() 
        var z=new class_3()
        println("Number of students in class 1 : " +  x.NumberOfStudents()) 
        println("Number of students in class 2 : " +  y.NumberOfStudents())
        println("Number of students in class 3 : " +  z.NumberOfStudents()) 
  }
}

No comments:

Post a Comment