This document provides an introduction to coding fundamentals and object-oriented programming concepts like classes, inheritance, encapsulation and polymorphism. It demonstrates basic coding concepts like conditionals, loops and methods in Ruby and Java. It also uses human classes and inheritance as an example to illustrate OOP principles, showing how classes can model real-world things and their attributes and behaviors.
7. The start of a new day, in Ruby.
if alarm.is_ringing? # Asking alarm if it is ringing
self.wake_up() # Waking up
self.do_gymnastics(1.minute)
if self.need_to_go? # Asking yourself.
car.start_engine()
if self.saw(stopSign) # One more conditional
self.stop_car()
end
end
end
8. Nouns vs Verbs.
Nouns are objects.
Nouns can do things.
The verb is called a method or function.
The verb actually do things.
11. Classes.
class Human
def initialize(name, intelligence)
@name = name
@intelligence = intelligence
end
end
adam = Human.new( "Adam", 10 )
eve = Human.new( "Eve", 9 )
3000000000.times do | i |
Human.new( "Eve #" + i.to_s, rand(100) )
end
13. Inheritance.
class Woman < Human
attr_accessor :surname
def gossip()
@intelligence = @intelligence + 1
end
end
class Man < Human
def gossip()
@intelligence = @intelligence - 10
end
end
15. Encapsulation.
class Woman < Human
attr_accessor :surname
def gossip()
@intelligence = @intelligence + 1
end
end
class Man < Human
def gossip()
@intelligence = @intelligence - 10
end
end
17. Polymorphism.
class Woman < Human
attr_accessor :surname
def gossip()
@intelligence = @intelligence + 1
end
end
class Man < Human
def gossip()
@intelligence = @intelligence - 10
end
end
18. Object Oriented Programming at a
glance.
Together:
¡ñ Encapsulation
¡ñ Inheritance
¡ñ Polymorphism
make up the fundament of OOP.