Extending a Class in Scala
Extending a class in Scala user can design an inherited class. To extend a class in Scala we use extends keyword. there are two restrictions to extend a class in Scala :
- To override method in scala override keyword is required.
- Only the primary constructor can pass parameters to the base constructor.
Syntax:
class base_class_name extends derived_class_name { // Methods and fields }Example:
// Scala program of extending a class// Base classclassGeeks1{varName:String="chaitanyashah"}// Derived class// Using extends keywordclassGeeks2extendsGeeks1{varArticle_no:Int=30// Methoddefdetails(){println("Author name: "+ Name);println("Total numbers of articles: "+ Article_no);}}// Creating objectobjectGFG{// Driver codedefmain(args:Array[String]){// Creating object of derived classvalob=newGeeks2();ob.details();}}Output:
Author name: chaitanyashah Total numbers of articles: 30
In the above example Geeks1 is the base class and Geeks2 is the derived class which is derived from Geeks1 using extends keyword. In the main method when we create the object of Geeks2 class, a copy of all the methods and fields of the base class acquires memory in this object. That is why by using the object of the derived class we can also access the members of the base class.
Example:
// Scala program of extending a class// Base classclassParent{varName1:String="geek1"varName2:String="geek2"}// Derived from the parent classclassChild1extendsParent{varAge:Int=32defdetails1(){println(" Name: "+ Name1)println(" Age: "+ Age)}}// Derived from Parent classclassChild2extendsParent{varHeight:Int=164// Methoddefdetails2(){println(" Name: "+ Name2)println(" Height: "+ Height)}}// Creating objectobjectGFG{// Driver codedefmain(args:Array[String]){// Creating objects of both derived classesvalob1=newChild1();valob2=newChild2();ob1.details1();ob2.details2();}}Output:
Name: geek1 Age: 32 Name: geek2 Height: 164
In the above example Parent is the base class Child1 and Child2 are the derived class which is derived from Parent using extends keyword. In the main method when we create the objects of Child1 and Child2 class a copy of all the methods and fields of the base class acquires memory in this object.
Example:// Scala program of extending a class// Base classclassBicycle (valgearVal:Int,valspeedVal:Int){// the Bicycle class has two fieldsvargear:Int=gearValvarspeed:Int=speedVal// the Bicycle class has two methodsdefapplyBreak(decrement:Int){gear=gear - decrementprintln("new gear value: "+ gear);}defspeedUp(increment:Int){speed=speed + increment;println("new speed value: "+ speed);}}// Derived classclassMountainBike(overridevalgearVal:Int,overridevalspeedVal:Int,valstartHeightVal:Int)extendsBicycle(gearVal, speedVal){// the MountainBike subclass adds one more fieldvarstartHeight:Int=startHeightVal// the MountainBike subclass adds one more methoddefaddHeight(newVal:Int){startHeight=startHeight + newValprintln("new startHeight : "+ startHeight);}}// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){valbike=newMountainBike(10,20,15);bike.addHeight(10);bike.speedUp(5);bike.applyBreak(5);}}Output:
new startHeight : 25 new speed value: 25 new gear value: 5
In above program, when an object of MountainBike class is created, a copy of the all methods and fields of the superclass acquire memory in this object.


