CSC/ECE 517 Fall 2011/ch3 4b ms
Object oriented languages: An Overview
C++:
C++ is a statically typed, multi-paradigm programming language. It supports some features of object-oriented languages like encapsulation, polymorphism and inheritance. It provides access control by specifiers public, private and protected and also allows “friend” classes. C++ allows multiple inheritance which is eliminated in most other programming languages as it allows a class to inherit from more than one base class which results in an ambiguity effect.
Java:
Java is classified as a statically typed, hybrid object oriented language. Although it provides information hiding, inheritance and polymorphism, it has a few basic types incorporated that are not objects and some of the built-in operators are provided by basic arithmetic and not as messages to objects. [link]. For these reasons, it is not considered to be a “pure” object-oriented language.
Java provides access control and supports private, public and protected access. It also allows to create packages to create and maintain namespaces. Java is designed not to provide multiple inheritance to avoid problems faced in C++. However, it presents a single-rooted hierarchy where “Object” is the ancestor of all classes (seen in Ruby too) and provides interfaces to extend/inherit functionality.
Python:
Python is a dynamically typed, interpreted, object oriented language. It is argued that Python is not completely object oriented as it does not provide access control.
Ruby:
Ruby is dynamically typed and highly object-oriented, much more than Java and Python. In Ruby, it is said that “everything is an object”. Every value happens to be an object in Ruby. You may call methods on a string, an integer just like you call on any object in Java.
Ruby : Purely Object Oriented
Ruby is considered to be a purely Object oriented Language, as everything in it is considered to be an object. Unlike Java, even primitives such as characters, punctuation, numbers and strings are treated as objects in Ruby. Ruby was designed to facilitate the enforcement of Object Oriented Methods. Ruby is a Dynamically Typed Language.
Object oriented Concepts in ruby:
Ruby is a singly rooted Object Oriented Programming language which has all its classes having the same superclass which is the class Object.
Classes:
A class is used to define a blueprint of a data type. It does not contain any data, but it specifies what kind of variables and methods an object of the class will have. A class in Ruby is defined using the keyword class followed by the classname. The variables and methods are defined within the class definition and the class definition terminates with the end keyword.
For ex: class Rectangle
def initialize(w,l) @width, @length = w, l end
end Here, the initialize method is used to set the values of the instance variables (@width and @length. The instance variables are the properties of the objects created of this class type. These instance variables can be accessed within the class using an @. These variables can be accessed from outside the class using the accessor methods which are defined inside the class.
In the following case printLength and printWidth are public methods used to access the instance variables from outside the class. These are called accessor methods. Similarly, to
class Rectangle
# constructor method def initialize(w,l) @width, @length = w, l end
# accessor methods def printWidth @width end
def printLength @length end
- setter methods
def setWidth=(value) @width = value end def setHeight=(value) @height = value end
end
- create an object
rectagle = Rectangle.new(10, 20)
- use accessor methods
x = rectangle.printWidth() y = rectangle.printLength()
puts "Width of the Rectangle is : #{x}" puts "Length of the Rectangle is : #{y}" Class Methods and Class Variables: A class variable is shared between all instances of the class. The value of the class variable remains the same for all the objects of the class. A class variable is accessed within the class using two @ signs at the beginning of the variable name. The class variables must be initialized within the class.
The class methods are defined using def self.methodname() and ends with an end delimiter. These methods are called using the class name itself, for example: classname.methodname
Attributes and accessor methods:
Every class in Ruby defines a set of attributes for its objects. Ruby provides accessor methods (famously known as getter-setter methods in Java) to access these variables. Ruby provides three kinds of accessor methods:
Class Car
attr_accessor : name, company attr_reader: price attr_writer: color
end
attr_accessor creates a setter and getter method. The attr_reader method creates only the getter method. This is required in case of attributes that are not to be modified by other programs but their value may be viewed. The attr_writer creates the setter method. In case of Java, the setter and getter methods need to be written in 3 lines of code each whereas in Ruby, the methods are written just by specifying the type of accessor, which have already been coded using Metaprogramming.[link]
Access Control:=
Just like any other o-o programming language, Ruby allows to exercise control over the attributes and methods of a class. Ruby provides 3 levels of protection at instance methods level. Public: In this level objects from outside the class can call the method. All methods except initialize are public by default. Private: These methods can be call only by the objects of the same class. Initialize methods are private by default. Protected: These methods can be called either by objects of the same class or of the class derived from this class.
The methods are given access levels using the following syntax. Private :method1, :method2
Abstract methods
Ruby is a dynamically typed language and does not provide abstract classes as in Java or C++. It instead provides modules that can be included within any class. The reason why Ruby does not provide abstract classes is because of the idea that it only matters if two objects can perform a function, they don’t necessarily have to be inherited from the same class. This paradigm is provided by modules, where the method of a module can be redefined as required by a class that includes it.
Also, if required the technique of abstract class can be simulated by something like this:
Class Vehicle
def drive
raise NotImplementedError.new("Method not implemented") end end
The subclasses that inherit from the above class will have to redefine the method “drive” as required.