abstract class Pet (name: String) {
def speak: Unit = println(s"My name is $name")
}
class Dog(name: String) extends Pet(name)
val d = new Dog("Fido")
d.speak
// Scala program to illustrate how to
// create an abstract class
// Abstract class
abstract class myauthor
{
// abstract method
def details()
}
// GFG class extends abstract class
class GFG extends myauthor
{
def details()
{
println("Author name: Ankita Saini")
println("Topic name: Abstract class in Scala")
}
}
object Main
{
// Main method
def main(args: Array[String])
{
// objects of GFG class
var obj = new GFG()
obj.details()
}
}
// Scala program of abstract type member
// Declaring an abstract class
abstract class vehicle (name:String)
{
// This is an abstract member
// with undefined implementation
val category: String
// A function that is used to print
// the value of the abstract member
def car_type{ println(category) }
override def toString = s" The vehicle type is $category"
}
// Now extend the classes bike, car,
// truck and bus and provide values
// for the variable type
class car (name:String) extends vehicle (name)
{
// assigning the value of
// the abstract member as car
val category = "car"
}
class bike (name:String) extends vehicle (name)
{
// assigning the value of
// the abstract member as bike
val category = "bike"
}
class bus (name:String) extends vehicle (name)
{
// assigning the value of
// the abstract member as bus
val category = "bus"
}
class truck (name:String) extends vehicle (name)
{
// assigning the value of
// the abstract member as truck
val category = "truck"
}
object AbstractFieldsDemo extends App
{
// assigning the name as Honda in the abstract
// class where the category value is car
val car = new car("Honda")
// assigning the name as Yamaha in the abstract
// class where the category value is bike
val bike = new bike("Yamaha")
// assigning the name as Tata in the abstract
// class where the category value is bus
val bus = new bus("Tata")
// assigning the name as Ashok_Leyland in the
// abstract class where the category value is truck
val truck = new truck("Ashok_Leyland")
// implementing the function
// car_type for the object car
car.car_type
// implementing the function
// car_type for the object bus
bus.car_type
// implementing the function
// car_type for the object truck
truck.car_type
// implementing the function
// car_type for the object bus
bike.car_type
println(car)
println(bus)
println(truck)
println(bike)
}
// Keywords
<- // Used on for-comprehensions, to separate pattern from generator
=> // Used for function types, function literals and import renaming
// Reserved
( ) // Delimit expressions and parameters
[ ] // Delimit type parameters
{ } // Delimit blocks
. // Method call and path separator
// /* */ // Comments
# // Used in type notations
: // Type ascription or context bounds
<: >: // Upper and lower bounds
<% // View bounds (deprecated)
" """ // Strings
' // Indicate symbols and characters
@ // Annotations and variable binding on pattern matching
` // Denote constant or enable arbitrary identifiers
, // Parameter separator
; // Statement separator
_* // vararg expansion
_ // Many different meanings
On the inside of a class, the name this represents the object on which the current method is executed.
Add the functions less and max to the class Rational.
class Rational(x: Int, y: Int) {
...
def less(that: Rational) =
numer * that.denom < that.numer * denom
def max(that: Rational) =
if (this.less(that)) that else this
}
Note that a simple name x, which refers to another member of the class, is an abbreviation of this.x. Thus, an equivalent way to formulate less is as follows.
These methods support program verification and runtime correctness.
final def assert(assertion: Boolean, message: => Any): Unit
Tests an expression, throwing an AssertionError if false.
def assert(assertion: Boolean): Unit
Tests an expression, throwing an AssertionError if false.
final def assume(assumption: Boolean, message: => Any): Unit
Tests an expression, throwing an AssertionError if false.
def assume(assumption: Boolean): Unit
Tests an expression, throwing an AssertionError if false.
final def require(requirement: Boolean, message: => Any): Unit
Tests an expression, throwing an IllegalArgumentException if false.
def require(requirement: Boolean): Unit
Tests an expression, throwing an IllegalArgumentException if false.
scalaVersion := "2.13.3"
scalacOptions ++= Seq(
"-encoding", "utf8", // Option and arguments on same line
"-Xfatal-warnings", // New lines for each options
"-deprecation",
"-unchecked",
"-language:implicitConversions",
"-language:higherKinds",
"-language:existentials",
"-language:postfixOps"
)