1. extension classes
It is the same as using the extends keyword in the manner in Scala and Java extension class
The class can be declared as final, so they can not expand
Can also put a single method or field is declared as final, to ensure that it can not be rewritten, and pay attention to the different Java, Java in the final modified field means immutable.
5.3.2. Overriding methods
A rewritable non-abstract methods must be used in Scala override modifier
Call the superclass in a subclass, use super, Java and consistent.
5.3.3.protected modified fields and methods
The protected field or modified methods, which can be accessed subclasses;
The difference is that with Java, protected modified member of the class belongs to the package are not visible;
protected [this], restrict access to only the current object
You can use modifiers to change the package members protected modified visibility
5.3.3. Type checking and conversion
Scala |
Java |
|
To test whether an object belongs to a given class |
obj.isInstanceOf[C] |
obj instanceof C |
Cast |
obj.asInstanceOf[C] |
(C)obj |
Get Object class information |
classOf[C] |
C.class |
5.3.4. Superclass constructor
The following code defines a subclass and a superclass constructor call primary constructor
class Employ(name: String, age: Int, val Salary:Double) extends Person (name,age)
The following code shows the class inheritance, as well as implementation of the interface, etc.
Object ClassDemo { DEF main (args: the Array [String]) { Val H = new new Human the println (H. byScala ()) } } // equivalent Java interface trait Coding { DEF byJava (In Flag: Boolean): Unit = { IF (Flag) { println ( "the I CAN code by the Java" ) } the else { println ( "the I CAN not by the Java code" ) } } method is not implemented by default // abstract method DEF byScala (): String } // abstract class
abstract class Animal { DEF RUN (): Int Val name: String } class Human the extends Animal with Coding { Val name = "ABC" ? // Print times "the ABC" Val T1, T2, (A, B, C) = { the println ( "the ABC" ) ( . 1 , 2 , . 3 ) } the println (a) the println (t1._ . 1 ) // a non-abstract methods must be overridden in Scala modified with the override the override DEF byJava (in Flag: Boolean): Unit = {
println ( "Coding by the Java and Scala" ) } DEF byScala (): String = { "Coding by Scala" } // When overridden abstract superclass, do not need to use the override keyword in a subclass, also wrote may DEF RUN (): = {Int . 1 } }