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