OOP Flashcards

1
Q

Polymorphism

A

Полиморфизм дает возможность дляразныхтипов использоватьодинкод.
Полиморфизм можно грубо разделить на динамический и статический.

Динамический полиморфизм — это про абстрактные классы, интерфейсы, утиную типизацию, т.е. только в рантайме будет понятно, с каким типом будет работать наш код

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Ruby constructor

A

A constructor is a special kind of a method. It is automatically called when an object is created. Constructors do not return values. The purpose of the constructor is to initiate the state of an object. The constructor in Ruby is called initialize. Constructors do not return any values.
The constructor of a parent object is called with a super method. They are called in the order of inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ruby constructor overloading

A

Constructor overloading is the ability to have multiple types of constructors in a class. This way we can create an object with different number or different types of parameters.

Ruby has no constructor overloading that we know from some programming languages. This behaviour can be simulated to some extent with default parameter values in Ruby.

ex

class Person

    def initialize name="unknown", age=0
        @name = name
        @age = age        
    end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Ruby access modifiers

A

Access modifiers set the visibility of methods and member fields. Ruby has three access modifiers: public, protected and private. In Ruby, all data members are private. Access modifiers can be used only on methods. Ruby methods are public, unless we say otherwise.

Only two things are important. First, if we call the method inside or outside the class definition. Second, if we use or do not use the self keyword which points to the current receiver.

Access modifiers protect data against accidental modifications.

Private methods are tightest methods in Ruby. They can be called only inside a class definition and without the self keyword.

Protected methods are like private. There is only one small difference. They can be called with the self keyword specified. Protected methods cannot be called outside the class definition.

The output of the example confirms that in Ruby language, public, protected, private methods and private member fields are inherited by child objects from their parents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Ruby super method

A

The super method calls a method of the same name in the parent’s class. If the method has no arguments it automatically passes all its arguments. If we write super() no arguments are passed to parent’s method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Methods Overriding

A
Though you can add new functionality in a derived class, but sometimes you would like to change the behavior of already defined method in a parent class. You can do so simply by keeping the method name same and overriding the functionality of the method as shown below in the example −
`# define a class
class Box
   # constructor method
   def initialize(w,h)
      @width, @height = w, h
   end
   # instance method
   def getArea
      @width * @height
   end
end
# define a subclass
class BigBox < Box
   # change existing getArea method as follows
   def getArea
      @area = @width * @height
      puts "Big box area is : #@area"
   end
end`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Operator Overloading

A

Ruby permits operator overloading, allowing one to define how an operator shall be used in a particular program. For example a ‘+’ operator can be define in such a way to perform subtraction instead addition and vice versa. The operators that can be overloaded are +, -, /, *, **, %, etc and some operators that can not be overloaded are &, &&, |, ||, (), {}, ~, etc.

Operator functions are same as normal functions. The only differences are, name of an operator function is always symbol of operator followed operator object. Operator functions are called when the corresponding operator is used. Operator overloading is not commutative that means that 3 + a is not same as a + 3. When someone tries to run 3 + a, it will fail.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Freezing Objects

A

Sometimes, we want to prevent an object from being changed. The freeze method in Object allows us to do this, effectively turning an object into a constant. Any object can be frozen by invoking Object.freeze. A frozen object may not be modified: you can’t change its instance variables.

You can check if a given object is already frozen or not using Object.frozen? method, which returns true in case the object is frozen otherwise a false value is return.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create Object Using Allocate

A

There may be a situation when you want to create an object without calling its constructor initialize i.e. using new method, in such case you can call allocate, which will create an uninitialized object for you

ex:
Box.allocate
 => #
Box.new(10, 20)
 => #
How well did you know this?
1
Not at all
2
3
4
5
Perfectly