shankar524 / ruby_guide

Provide Ruby onboarding to non Ruby programmers.
0 stars 0 forks source link

OOP tutorial #1

Open shankar524 opened 4 years ago

shankar524 commented 4 years ago

Need to add OOP tutorials, topics needed:-

IlyaLukin commented 4 years ago

1. Classes Example # Class is the piece of code where we define the attributes and/or behaviors of an object. You can define variables, constants, methods and constructors to the object, inside the class. In another words, class is the blueprint of an object. Let's see a sample class in Java, which defines a (simple) Car: public class Car { private Engine engine; private Body body; private Tire [] tire; private Interior interior;

// Constructor public Car (Engine engine, Body body, Tire[] tires, Interior interior) {

}

// Another constructor public Car () {

}

public void drive(Direction d) { // Method to drive }

public void start(Key key) { // Start } } This is just for an example. You can model real world object like this, as per your requirement.

IlyaLukin commented 4 years ago

2. Objects Example # Object is the base module in the Object Oriented Programming (OOP). An Object can be a variable, data structure (like an array, map, etc), or even a function or method. In OOP, we model real world objects like animals, vehicles, etc. An object can be defined in a class, which can be defined as the blueprint of the object. Then we can create instances of that class, which we call objects. We can use these objects, their methods and variables in our code after.

IlyaLukin commented 4 years ago

3.Inheritance - Definition Example # Inheritance is one of the main concepts in Object Oriented Programming (OOP). Using inheritance, we can model a problem properly and we can reduce the number of lines we have to write. Let's see inheritance using a popular example. Consider you have to model animal kingdom (Simplified animal kingdom, of course. Biologists, pardon me) using OOP. There are a lots of species of animals, some have unique features, while some share same features. There a are main families of animals. Let's say, Mammals, Reptiles. Then we have children of those families. For an example, Cat, Dog, and Lion are mammals. Cobra and Python are reptiles. Every animal shares some basic features like eat, drink, move. Hence we can say that we can have a parent called Animal from which they can inherit those basic features. Then those families also shares some features. For an example reptiles use crawling to move. Every mammal is fed milk at early stages of life. Then there are some unique features for each and every animal. Consider if we are to create these animal species separately. We have to write same code again and again in every animal species. Instead of that, we use inheritance. We can model the Animal Kingdom as follows: We can have parent Object called Animal, which have basic features of all the animals. Mammal and Reptile (of course the other animal families also) objects with their common features while inheriting the basic features from parent object, Animal. Animal species objects: Cat and Dog inherits from Mammalobject, Cobra and Python inherits from Reptile object, and so on. In this form we can reduce the code we write, as we do not need to define basic features of Animals in each animal species, as we can define them in the Animal object and then inherit them. Same thing goes with the animal families.

IlyaLukin commented 4 years ago

4. Inheritance Example - Consider below two classes Example # Teacher Class: class Teacher { private String name; private double salary; private String subject; public Teacher (String tname) { name = tname; } public String getName() { return name; } private double getSalary() { return salary; } private String getSubject() { return subject; } } OfficeStaff Class: class OfficeStaff{ private String name; private double salary; private String dept; public OfficeStaff (String sname) { name = sname; } public String getName() { return name; } private double getSalary() { return salary; } private String getDept () { return dept; } } Both the classes share few common properties and methods. Thus repetition of code. Creating a class which contains the common methods and properties. The classes Teacher and OfficeStaff can inherit the all the common properties and methods from below Employee class. Employee Class: class Employee{ private String name; private double salary; public Employee(String ename){ name=ename; } public String getName(){ return name; } private double getSalary(){ return salary; } } Add individual methods and properties to it Once we have created a super class that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses Any similar classes like Engineer, Principal can be generated as subclasses from the Employee class. The parent class is termed super class and the inherited class is the sub class A sub class is the specialized version of a super class – It inherits all of the instance variables and methods defined by the super class and adds its own, unique elements. Although a sub class includes all of the members of its super class it can not access those members of the super class that have been declared as private. A reference variable of a super class can be assigned to a reference to any sub class derived from that super class i.e. Employee emp = new Teacher();

IlyaLukin commented 4 years ago

5.Method Overloading Example # Method overloading is the way of using polymorphism inside a class. We can have two or more methods inside the same class, with different input parameters. Difference of input parameters can be either: Number of parameters Type of parameters (Data type) Order of the parameters Let's take a look at them separately (These examples in java, as I am more familiar with it - Sorry about that): Number of Parameters public class Mathematics { public int add (int a, int b) { return (a + b); }

 public int add (int a, int b, int c) {
     return (a + b + c); 
 }

 public int add (int a, int b, int c, int c) {
     return (a + b + c + d);
 }

} Look carefully, you can see the method's return type is the same - int, but theree of these methods having different number of inputs. This is called as method over loading with different number of parameters. PS: This is a just an example, there's no need to define add functions like this. Type of parameters public class Mathematics { public void display (int a) { System.out.println("" + a); }

public void display (double a) {
    System.out.println("" + a);
}

public void display (float a) {
    System.out.println("" + a);
}

} Note that every method has the same name and same return type, while they have different input data types. PS: This example is only for explaining purpose only. Order of the parameters public class Mathematics { public void display (int a, double b) { System.out.println("Numbers are " + a + " and " + b); }

public void display (double a, int b) {
    System.out.println("Numbers are " + a + " and " + b);
}

} PS: This example is also for explaining purpose only.

IlyaLukin commented 4 years ago

6. Method Overriding Example # Method overriding is the way of using polymorphism between classes. if one class is inherited from another, the former (sub class) can override the latter's (super class's) methods, and change the implementation. this is used where the super class defines the more general implementation of the method while the sub class uses a more specific one. Consider following example: We have a class for Mammals: class Mammal { void whoIam () { System.out.println("I am a Mammal"); } } Then we have a class for Dog, which is a Mammal: class Dog extends Mammal { @Override void whoIam () { super.whoIam(); System.out.println("I am a Dog!"); } } In this example, we define whoIam() method in Mammal class, where the mammal say it is a Mammal. But this is a general term, as there are a lot of Mammals out there. Then we can inherit Dog class from Mammal class, as Dog is a Mammal. But, to be more specific, Dog is a Dog as well as a Mammal. Hence, Dog should say, I am a Mammal and also I am a Dog. Hence we can Override the whoIam() method in super class (Mammal class, that is) from sub class (the Dog class). We can also call the super class's method using super.whoIam() in Java. Then, the Dog will behave like a Dog, while also behaving like a Mammal.

shankar524 commented 4 years ago

@rikoshet2018 I really appreciate your work here. Solution I was expecting was for Ruby based as this repo is for Ruby onboarding. But I loved the way you helped explain in Java. I would appreciate if you could have crated a Pull Request for it. Thanks by the way.

IlyaLukin commented 4 years ago

@shankar524 Okey, no problem)