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 derived_class_name extends base_class_name
{
// Methods and fields
}
Example:
class Geeks 1
{
var Name : String = "chaitanyashah"
}
class Geeks 2 extends Geeks 1
{
var Article _ no : Int = 30
def details()
{
println( "Author name: " + Name);
println( "Total numbers of articles: " + Article _ no);
}
}
object GFG
{
def main(args : Array[String])
{
val ob = new Geeks 2 ();
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:
class Parent
{
var Name 1 : String = "geek1"
var Name 2 : String = "geek2"
}
class Child 1 extends Parent
{
var Age : Int = 32
def details 1 ()
{
println( " Name: " + Name 1 )
println( " Age: " + Age)
}
}
class Child 2 extends Parent
{
var Height : Int = 164
def details 2 ()
{
println( " Name: " + Name 2 )
println( " Height: " + Height)
}
}
object GFG
{
def main(args : Array[String])
{
val ob 1 = new Child 1 ();
val ob 2 = new Child 2 ();
ob 1 .details 1 ();
ob 2 .details 2 ();
}
}
|
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:
class Bicycle ( val gearVal : Int, val speedVal : Int)
{
var gear : Int = gearVal
var speed : Int = speedVal
def applyBreak(decrement : Int)
{
gear = gear - decrement
println( "new gear value: " + gear);
}
def speedUp(increment : Int)
{
speed = speed + increment;
println( "new speed value: " + speed);
}
}
class MountainBike( override val gearVal : Int,
override val speedVal : Int,
val startHeightVal : Int)
extends Bicycle(gearVal, speedVal)
{
var startHeight : Int = startHeightVal
def addHeight(newVal : Int)
{
startHeight = startHeight + newVal
println( "new startHeight : " + startHeight);
}
}
object GFG
{
def main(args : Array[String])
{
val bike = new MountainBike( 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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Jun, 2023
Like Article
Save Article