Reuseability

Recycle, Reduce, Reuse

August 24, 2014


Reusability is extremely important when creating DRY code. There are two widely used ways to reuse the code of existing objects. These are inheritance and composition. So what’s the difference?

What is Inheritance?

According to the widely accepted dictionary that is wikipedia, inheritance is “a way to reuse code of existing objects, or to establish a subtype from an existing object, or both”

The inheritance method basically states that the subclass with inherit all or most of the superclass plus it’s own methods. Any action that you do to the subclass works as though they were done to an instance of the superclass. For this reason you put common functionality in the superclass and then specialize it for the subclass. There are three types of inheritance: implicit, override explicitly, and Alter Before and After, but as you will see inheritance can often grow out of hand and be difficult to test.

Implicit Inheritance

Implicit inheritance creates one super “Parent” class and all subclasses implicitly inherit those methods.

Example:



class Father
	attr_accessor :last_name
	
	def initialize(last_name)
		@last_name = last_name
	end
	
	def last_name
		puts "Your last name is #{@last_name.capitalize}."
	end
end

class Daughter < Father

end

last_name = "Dirks"
dad = Father.new(last_name)
dani = Daughter.new(last_name)
dad.last_name
dani.last_name


Result:

Your last name is Dirks.
Your last name is Dirks.

As you can see, the Daughter subclass inherits the method last_name from the Father class. last_name can be called on daughter now using implicit inheritance.

Override Explicitly

Calling functions implicity can be a problem when you want your subclass to behave differently than their superclass. In the case of our Father Daughter example if we wanted the Daughter to have a different last name we would have to override the last_name function in the Daughter subclass.



class Father
	attr_accessor :last_name
	
	def initialize(last_name)
		@last_name = last_name
	end
	
	def last_name
		puts "Your last name is #{@last_name.capitalize}."
	end

	
end

class Daughter < Father

	def last_name
		@last_name = "K"
		puts "Your last name is #{@last_name.capitalize}."
	end

end

last_name = "Dirks"
dad = Father.new(last_name)
dani = Daughter.new(last_name)
dad.last_name
dani.last_name

In this case our results are:

Your last name is Dirks.

Your last name is K.

We changed the last name of the Daughter class by overriding the last_name method within the subclass. This doesn’t seem very practical for future uses of the subclass however.

Alter Before or After

The last way to use inheritance is if you want to alter behavior before or after the superclass runs.You do this by overriding the function like in the override explicitly method and then use the build in super function to call the superclass function.



class Father
	attr_accessor :last_name
	
	def initialize(last_name)
		@last_name = last_name
	end
	
	def last_name
		puts "Your last name is #{@last_name.capitalize}."
	end

	
end

class Daughter < Father

	def last_name
		@last_name = "K"
		puts "Your last name is #{@last_name.capitalize}."
		super
		@last_name = "P"
		puts "Your last name is #{@last_name.capitalize}."
	end
end

last_name = "Dirks"
dad = Father.new(last_name)
dani = Daughter.new(last_name)
dad.last_name
dani.last_name

The result you get from this is:

Your last name is Dirks.

Your last name is K.

Your last name is K.

Your last name is P.

If you put in the Daughter subclass like this:



class Daughter < Father

	def last_name
		puts "Your last name is #{@last_name.capitalize}."
		super
		@last_name = "P"
		puts "Your last name is #{@last_name.capitalize}."
	end
end

s

Your result becomes:

Your last name is Dirks.

Your last name is Dirks.

Your last name is P.

I still don’t get why you would want to use this format of inheritance.

Composition:

Composition is a technique by which classes may achieve polymorphic behavior and code reuse by containing other classes that implement the desired functionality instead of through inheritance.

In using composition, rather than rewriting and overriding already built code, you can call the code that you need by calling on modules in your methods.