-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Inheritance in Ruby | ||
|
||
class Person | ||
|
||
attr_accessor:name, :age, :trait | ||
|
||
end | ||
|
||
class Sportsman < Person | ||
|
||
attr_accessor:sports | ||
|
||
end | ||
|
||
sp = Sportsman.new | ||
|
||
sp.age = 20 | ||
sp.name = "Filly" | ||
sp.sports = "Basketball" | ||
|
||
puts sp.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Inheritance in Ruby | ||
|
||
class Animal | ||
|
||
attr_accessor:color, :name | ||
|
||
end | ||
|
||
class Tiger < Animal | ||
|
||
def speak | ||
return "Grrrr....!!" | ||
|
||
end | ||
|
||
end | ||
|
||
tiger = Tiger.new | ||
|
||
tiger.color = "Blue" | ||
|
||
tiger.name = "Filly" | ||
|
||
puts tiger.inspect | ||
|
||
puts tiger.speak |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Inheritance in Ruby | ||
|
||
class Animal | ||
|
||
attr_accessor:color, :name | ||
|
||
def initialize(name, color) | ||
|
||
@name = name | ||
|
||
@color = color | ||
|
||
end | ||
|
||
end | ||
|
||
class Tiger < Animal | ||
|
||
def speak | ||
return "Grrrr....!!" | ||
|
||
end | ||
|
||
end | ||
|
||
tiger = Tiger.new("Filly", "Blue") | ||
|
||
puts tiger.inspect | ||
|
||
puts tiger.speak |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Inheritance in Ruby | ||
|
||
class Animal | ||
|
||
attr_accessor:color, :name | ||
|
||
end | ||
|
||
class Tiger < Animal | ||
|
||
def initialize(name, color) | ||
|
||
@name = name | ||
|
||
@color = color | ||
|
||
end | ||
|
||
def speak | ||
return "Grrrr....!!" | ||
|
||
end | ||
|
||
end | ||
|
||
tiger = Tiger.new("Filly", "Blue") | ||
|
||
puts tiger.inspect | ||
|
||
puts tiger.speak |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Inheritance in Ruby | ||
|
||
class Animal | ||
|
||
attr_accessor:color, :name | ||
|
||
def identify | ||
|
||
return "My name is #{@name} and I am from #{self.class}" | ||
|
||
end | ||
|
||
end | ||
|
||
class Tiger < Animal | ||
|
||
def initialize(name, color) | ||
|
||
@name = name | ||
|
||
@color = color | ||
|
||
end | ||
|
||
def speak | ||
return "Grrrr....!!" | ||
|
||
end | ||
|
||
end | ||
|
||
tiger = Tiger.new("Filly", "Blue") | ||
|
||
animal = Animal.new | ||
|
||
puts tiger.inspect | ||
|
||
puts tiger.speak | ||
|
||
puts tiger.identify | ||
|
||
puts animal.inspect | ||
|
||
puts animal.identify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Method Overriding - Inheritance in Ruby | ||
|
||
class ParentArea | ||
|
||
# Constructor | ||
def initialize(w, h) | ||
|
||
@width = w | ||
@height = h | ||
|
||
end | ||
|
||
# To calculate the area from Parent Class | ||
def getArea | ||
|
||
return "Area from Parent class is #{@width*@height}" | ||
|
||
end | ||
|
||
end | ||
|
||
class ChildArea < ParentArea | ||
|
||
# To calculate the area from Child Class | ||
def getArea | ||
|
||
puts super() | ||
|
||
return "Area from Child class is #{@width*@height}" | ||
|
||
end | ||
|
||
end | ||
|
||
childobj = ChildArea.new(10, 20) | ||
|
||
puts childobj.getArea | ||
|