CSC/ECE 517 Fall 2011/ch4 4a ga
Introduction
This article discusses the extend concept as illustrated by Ruby's extend method. We provide a definition of extend, compare it to the include method, and go over the differences between extending a class and extending an object. We then consider two practical examples of Ruby's extend method.
From there, we go on to discuss how other languages provide functionality similar to that of Ruby's extend method. We also compare these implementations to the Ruby implementation as to whether they are more or less powerful than Ruby's. Finally, we also consider prototype-based languages such as Self, which don't even provide classes.
Extend in Ruby
What is Extend?
Definition
Extend vs. Include
Extending classes vs. Extending objects
Practical Examples
Twitter Module
Active::Concern
Singleton Pattern
Extend in other languages
Extend in Java
Java, like other object-orientated languages, supports class inheritance. Inheritance allows one class to "inherit" the properties of another class. Inheritance offers a radically different outlook on the re-use of code. Extend is a functionality that provides a way to inherit the fields and methods of a class by other class. This is done by extending the the class or implementing interfaces. This concept of extending the functionality is called Inheritance. In Java, when we wish to extend the usefulness of a class, we can create a new class that inherits the attributes and methods of another. We don't need a copy of the original source code to extend the usefulness of a library. We simply need a compiled '.class' file, from which we can create a new enhancement. Furthermore, instead of re-writing a class to provide a new function, we can simply declare we wish to extend a class, and add a few lines of code to get an enhancement. It makes the process of re-using code much simpler, and code easier to read. When we extend an existing class, we 'inherit' all of the attributes, and methods, of the parent class which are not static, final or private. This makes programming much easier, and simpler, as we don't re-invent the wheel each time we extend other classes.
For example, all Java objects are inherited from the java.lang.Object class. This means that we can call the toString() method inherited from java.lang.Object, and get a string representation of any java object, such as an Integer, a Float, a Double, etc. Take a look at the following example, which demonstrates the use of the 'extends' keyword.
public class A extends java.lang.Object { public int number; public String toString() { return new String("Value : " + number); } }
In this example, we implicitly state that we extend java.lang.Object, and override the functionality of the toString() method (present in java.lang.Object class) by providing our own function. Note that all classes, whether they state so or not, will be inherit from java.lang.Object. Our next example shows a more common use of the keyword 'extends'. In this example, we inherit from class A, which means that B will also contain a field called number, and a function called toString().
public class B extends A { public void increment() { number++; } } public class ABDemo { public static void main (String args[]) { // Create an instance of B B counter = new B(); // Increment B counter.increment(); // Call toString() method System.out.println ( counter.toString() ); } }
Even though we never defined a toString() method, or added a number field to the B class, it has inherited this information from A. This is a powerful feature of object-orientated programming, and can save significant time when developing classes.
In the above case, we can say that Object is base class and A is derived class or A is base class for B and B is derived class. Derived class always extends base class.An interface can also extend another interface. Extending a class is closely related to implementing an interface. A new class can extend at most one superclass, but it may implement several interfaces. Extending means adding new method definitions. Implementing means satisfying the existing interface contract by writing the proscribed method bodies. When a class or abstract class implements or extends, those methods are are available in all descendants. All descendant classes automatically implement that interface too.
The below program explains the ability of derived class to extend the fields and methods of base class.
class Box { double width; double height; double depth; Box() { } Box(double w, double h, double d) { width = w; height = h; depth = d; } void getVolume() { System.out.println("Volume is : " + width * height * depth); } } public class MatchBox extends Box { double weight; MatchBox() { } MatchBox(double w, double h, double d, double m) { super(w, h, d); weight = m; } public static void main(String args[]) { MatchBox mb1 = new MatchBox(10, 10, 10, 10); mb1.getVolume(); System.out.println("width of MatchBox 1 is " + mb1.width); System.out.println("height of MatchBox 1 is " + mb1.height); System.out.println("depth of MatchBox 1 is " + mb1.depth); System.out.println("weight of MatchBox 1 is " + mb1.weight); } }
Output
Volume is : 1000.0 width of MatchBox 1 is 10.0 height of MatchBox 1 is 10.0 depth of MatchBox 1 is 10.0 weight of MatchBox 1 is 10.0