# Class and inheritance

**URL:** <https://www.ruby-forum.com/t/class-and-inheritance/130809>\
**Category:** Ruby\
**Created:** [March 3, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/class-and-inheritance/130809 "2008-03-03T15:32:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![August0866](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@August0866](https://www.ruby-forum.com/u/August0866)\
**Post date:** [March 3, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/class-and-inheritance/130809/1 "2008-03-03T15:32:41Z")

</div>

Hi All,

I am not a programmer, yet. I am trying ruby on for size, most things  
fit ok, but Classes are puzzling me.

`
class Stats
`

---

<div class="post-metadata">

**Author:** ![August0866](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@August0866](https://www.ruby-forum.com/u/August0866)\
**Post date:** [March 3, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/class-and-inheritance/130809/2 "2008-03-03T19:49:07Z")

</div>

Classes in any OOP (object orientated language) follows the same basic  
laws  
of application.

A class would be the actual application or legal body of code. It must  
be  
noted that within a class body you can deï¬ne class methods and instance  
methods. This is what allows the application to actually function or  
operate.

Ruby EXAMPLE:

class Greeter

```
def initialize(name)
@name = name
end

def say(phrase)
puts "#{phrase}, #{@name}"

```

end  
end

g1 = Greeter.new(“Fred”)  
g2 = Greeter.new(“Wilma”)

g1.say(“Hello”) #=\> Hello, Fred  
g2.say(“Hi”) #=\> Hi, Wilma

Java EXAMPLE:

class HelloWorld {  
public static void main(String [] args) {

System.out.println(“hello world!!!”);

_Inheritance_ is the concept of a child class (sub class) automatically  
inheriting the variables and methods  
defined in its parent class (super class) This is known as inheritence.

EXAMPLE:

The _Shape class_ would be the parent class, and from that the _circle,  
square, and rectangle_ classes would be created or inherited.

The _Human class_ would be the the parent class, and from that the  
_man_,  
and _woman_ classes would be created or inherited.

\*Super class (Parent class) \*  
â€“ Any class above a specific class in the class hierarchy.

_Sub class (Child class)_  
â€“ Any class below a specific class in the class hierarchy.

\*In Ruby

\*Class deï¬nitions start with the keyword class followed by the class  
name  
(which  
must start with an uppercase letter).

_Below is a class called MyClass._

class MyClass  
def m1 # this method 1 is public  
end

protected  
def m2 # this method 2 is protected  
end

private  
def m3 # this method 3 is private  
end  
end
