abstract

Scala’s abstract keywords are abstract, just like Java’s, and abstract classes cannot be instantiated. In addition to classes that can be defined as abstract, the properties and methods of a class can also be abstract. From the following example, you can see that there is no difference between abstract class definitions in Java. Right

Var str1: String String 2: String = "str2" val str3: String String = "str2" String val str4: String = "str4" /** ** ** */ def printInfo1: Unit def printInfo2: The Unit = {println (s "${str1}, ${str2}, ${str3}, ${str4}")}}

inheritance

Like Java, inheritance uses the extends keyword, and the concepts are Java, including calling the constructor of the parent class first. Here are the differences:

  1. If you override a non-abstract method of a parent class, or a non-abstract variable, override is required in Scala, but in Java it is just a warning.
  2. An abstract method, an abstract variable defined by var, can be written instead of override, or it can be written.
  3. Override is an abstract variable defined by val.
Public class ScalaChild extends ScalaAbstract {public class ScalaAbstract extends ScalaAbstract {public class ScalaAbstract extends ScalaAbstract. String = "str1" /** ** String = "str2" /** ** must write override */ str3: String = "str3" /** * must write override */ override val str4: String = "str4" /** * can not write override */ def printInfo1: Unit = {println(s"${str1}, ${str2}, ${str3}, ${str4}")} Unit = {println(s"ScalaChild: ${str1}, ${str2}, ${str3}, ${str4}")}} object ScalaChild {def main(args: Array[String]): Unit = { val child = new ScalaChild() child.printInfo1 child.printInfo2 } }

polymorphism

Let’s start with the familiar Java. Defines an abstract class javaAnimal, containing a variable name and a method eat. JavaCat and Javadog are the implementation classes of JavaAnimal. Print with the eat method in the main method.

Public abstract class javaAnimal {public String name = "none "; abstract void eat(); } public class javaCat extends javaAnimal {String name = "cat "; Void eat() {System.out.println(" eat fish "); }} public class Javadog extends JavaAnimal {String name = "puppy "; Void eat() {System.out.println(" eat bone "); } } public class JavaAnimalMain { public static void eat(JavaAnimal animal) { System.out.println(animal.name); animal.eat(); } public static void main(String[] args) { eat(new JavaCat()); eat(new JavaDog()); }}

The printed result is:

No fish, no bones

You can see that the name is the value of the parent class, but the method is the method that calls the child class. In Scala, implement something similar:

Abstract class ScalaAnimal {var name: String = "no" def eat(): Unit} Object ScalaAnimal {def eat(): ScalaAnimal): Unit = { println("******") println(animal.name) animal.eat() } def main(args: Array[String]): Unit = {eat(new ScalaCat) eat(new Scaladog)}} class ScalaCat extends ScalaAnimal {override (new ScalaCat) eat(): } class Scaladog extends ScalaAnimal {Override def eat(): Override (); Override (); Override (); Unit = {println(" bone eating ")}}

Print results:

****** The cat eats fish while the dog eats bones

Name prints the name of the subclass. So in Java, properties are statically bound and methods are dynamically bound. In Scala, properties and methods are dynamically bound.