Inheritance is a good way to share functionality between objects. When a class has a parent class, we say it inherits the fields and methods of its parent.
Running the preceding code in the groovyConsole would print out “flap flap”. This way you can have a Bird class that also uses the Wing class for example.
Objectify
What is an object anyway? An object is an instance of a class (in Java, Groovy, and Scala). It can have state (fields, also known as instance variables) stored in memory.
Once an object is created, it can be passed around (this is called a pass by reference). Although String is a special class, it is a class, so you can pass around an instance of it, as shown in the preceding code.
JavaScript
All functions and objects in JavaScript have a prototype.
Parenting 101
A parent class defines shared functionality (methods) and state (fields) that are common to multiple classes. You can specify the visibility of fields and methods using access modifiers like public and protected (we will cover these more in depth later).
There are two classes in the preceding code, Griffon and Dragon, that extend FlyingCreature. FlyingCreature is sometimes referred to as the base class. Griffon and Dragon are referred to as subclasses.
Within each constructor of Griffon and Dragon, the keyword super refers to the parent class’s (FlyingCreature) constructor.
This concept is called extension, inheritance, or polymorphism. You extend the parent class (FlyingCreature, in this case).
JavaScript
In JavaScript, we can use prototypes to extend functionality.
Packages
In Java (and related languages, Groovy, and Scala), a package is a namespace for classes. Namespace is just shorthand for a bin of names (names can be reused if they are in different bins). Every modern programming language has some type of namespace feature. This is necessary, owing to the nature of having lots of classes in typical projects.
The Java file also needs to reside in the directory corresponding to the package, so in this case com/github/modernprog. Also, there is a common understanding that a package name typically corresponds to a URL (github.com/modernprog, in this case). However, this is not necessary.
Public Parts
You might be wondering why the word public shows up everywhere in the examples so far. The reason has to do with encapsulation. Encapsulation is a big word that just means “a class should expose as little as possible to get the job done” (some things are meant to be private). This helps reduce complexity of code and therefore makes it easier to understand and think about.
private: Only this class can see it.
protected: Only this class and its descendants can see it.
public: Everyone can see it.
There’s also “default” protection (absence of a keyword), which limits use to any class in the same package (package protected).
JavaScript
JavaScript does not have the concept of packages, but, instead, you must rely on scope. Variables are only visible inside the function they were created in, except for global variables. There are frameworks in JavaScript for providing something like packages, but they are outside the scope of this book. One is RequireJS1 which allows you to define modules and dependencies between modules.
Interfaces
An interface declares method signatures that will be implemented by classes that implement the interface. This allows Java code to work with several different classes without necessarily knowing what specific class is “underneath” the interface. An interface is something like a contract that says what an implementing class must implement.
JavaScript does not have an equivalent concept to interface; however, interfaces are not useful since JavaScript is not strongly typed. You can call any method you want.
Abstract Class
An abstract class is a class that can have abstract methods but cannot have instances. It is something like an interface with functionality. However, a class can only extend one superclass, while it can implement multiple interfaces.
Enums
values(): Gives you an array of all possible values in the enum (static)
valueOf(String): Converts the given string into the enum value with the given name (static)
name(): An instance method on the enum that gives its name
Annotations
Java annotations allow you to add meta-information to Java code that can be used by the compiler, various APIs, or even your own code at runtime. They can be put before definitions of methods, classes, fields, parameters, and some other places.
This is useful because it will cause a compile-time error if you mistype the method name or a parameter’s type for example. It’s not required to override a method, but it’s good practice to use it.
Other useful annotations are those in javax.annotation, such as @Nonnull and @Nonnegative , which can be added to parameters to declare your intentions and be used by an IDE to help catch bugs in code.
There are many other annotations used by frameworks like Hibernate, Spring Boot, and others that can be very useful. Annotations such as @Autowired and @Inject are used by direct-injection frameworks such as Spring and Google Guice2 to reduce “wiring” code.
Autoboxing
Although Java is an object-oriented language, this sometimes conflicts with its primitive types (int, long, float, double, etc.). For this reason, Java added autoboxing and unboxing to the language.
Autoboxing
The Java compiler will automatically wrap a primitive type in the corresponding object when it’s necessary, like int to Integer, boolean to Boolean, double to Double, and float to Float. For example, when passing in parameters to a function or assigning a variable, as in the following: Integer number = 1.
Unboxing
Unboxing is the reverse of autoboxing. The Java compiler will unwrap an object to the corresponding primitive type when possible. For example, the following code is acceptable: double d = new Double(1.1) + new Double(2.2).
Summary
Extension and composition
Public vs. private vs. protected vs. package protected
Class, abstract class, interface, and enum
Annotations
Autoboxing and unboxing