Scala Guide for Java Developers

Source: Internet
Author: User
Tags inheritance

Scala has not only introduced functional concepts to the JVM, it has also provided us with a modern perspective on object-oriented language design. In this part of the Scala guide for Java developers, Ted Neward describes how Scala leverages features (trait) to make objects simpler and more easily built. You will learn that there are similarities and differences between features and the traditional polarity provided by the Java™ interface and C + + multiple inheritance.

Sir Isaac Newton, a renowned scientist and researcher, has a famous saying: "If I look farther than others, it is because I stand on the shoulders of giants." As a zealous historian and political scientist, I would like to make a few changes to this great man's famous words: "If I see farther than others, it is because I stand on the shoulders of history." This statement also reflects another historian George Santayana's famous saying: "Forget history will repeat the same." In other words, if we cannot look back at history and learn from past mistakes (including our own past experiences), there is no chance of improvement.

You may wonder, what does such a philosophy have to do with Scala? Inheritance is one of the things we want to discuss. Consider the fact that the creation of the Java language was almost 20 years ago, when it was the "object-oriented" heyday. It was designed to mimic the prevailing language of C + +, trying to lure developers using the language into the Java platform. There is no doubt that such decisions were wise and necessary at the time, but, in retrospect, some of them were not as useful as the founders had imagined.

For example, 20 years ago, for the creator of the Java language, it was necessary to reflect the C + + style of private inheritance and multiple inheritance. Since then, many Java developers have begun to regret these decisions. In this issue of the Scala Guide, I reviewed the history of multiple and private inheritance in the Java language. Then you'll see how Scala has rewritten history to bring greater benefits to everyone.

Inheritance in C + + and the Java language

History is a fact that people are willing to record.

-Napoleon Bonaparte.

People working in C + + can recall that private inheritance is a way to get behavior from a base class without explicitly accepting is-a relationships. Marking a base class as "private" allows derived classes to inherit from the base class without actually becoming a base class. But private inheritance is one of the features that have not been widely used. It is unwise to inherit the idea of a base class and not be able to convert it down or up to the base class.

On the other hand, multiple inheritance is often seen as an essential element of object-oriented programming. When modeling the hierarchy of transport vehicles, seaplane undoubtedly needs to inherit boat (using its startengine () and Sail () methods) and Plane (using its startengine () and Fly () methods). Seaplane is both boat and Plane, isn't it?

Anyway, this is the idea at the height of C + +. As we move quickly to the Java language, we consider multiple inheritance to be as flawed as private inheritance. All Java developers will tell you that seaplane should inherit the floatable and Flyable interfaces (and perhaps also enginepowered interfaces or base classes). Inheriting an interface means being able to implement all the methods needed by the class without encountering the challenges of virtual multiple inheritance (when encountering this dilemma, figure out which base class's StartEngine () should be called when calling Seaplane's StartEngine () method.

Unfortunately, outright abandonment of private inheritance and multiple inheritance can make us pay dearly for code reuse. Java developers may be happy to be freed from virtual multiple inheritance, but the cost is that programmers often do hard and error-prone jobs.

Review reusable behavior

Things can be divided roughly into what may never happen and what is unimportant.

-william Ralph Inge

The JavaBeans specification is the foundation of the Java platform, and it brings many Java ecosystems as a basis for POJO. As we all know, the properties in Java code are managed by Get ()/set (), as shown in Listing 1:

Listing 1. Person POJO

//This is Java
public class Person
{
   private String lastName;
   private String firstName;
   private int age;

   public Person(String fn, String ln, int a)
   {
     lastName = ln; firstName = fn; age = a;
   }

   public String getFirstName() { return firstName; }
   public void setFirstName(String v) { firstName = v; }
   public String getLastName() { return lastName; }
   public void setLastName(String v) { lastName = v; }
   public int getAge() { return age; }
   public void setAge(int v) { age = v; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.