Introduction and need of OOP in development
Sun, 17 Nov 2024
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. (Martin Golding)
Before we go to OOP, lets understand a little about procedural programming.
Here we will understand the basic concepts. We will discuss them briefly later. This is just an overview.
name
, age
properties.walk
.class Human{
private String name;
private int age;
public Human(String name, int age) {
this.name = name;
this.age = age;
}
}
Human
class have 2 properties name
, age
.Human
with name = Saeed
, and age = 23
. It becomes an object of the Human class since we have assigned some values to each properties.sd
, So sd = Human(name = Saeed, age=23)
.jn = Human(name = John, age = 45)
.sd
and jn
are object of the Human
class.Human sd = new Human("Saeed", 23);
Human jn = new Human("John", 45);
class Human{
private String name;
private int age;
public Human(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age < 0) {
return;
}
else{
this.age = age;
}
}
}
Vehicle
class can have a method named move()
, but each subclass(discussed later) like Car
, Bike
may implement the move()
method differently.Vehicle
class and it will work.move()
will call the correct method based on the object it is holding.Vehicle car = new Car();
Vehicle bike = new Bike();
car.move();
bike.move();
Math.pow(double, double)
function. we can simple use it and we know what it does. But we don't know how it does. Don't be confuse with hiding(discussed later).Dog
class can be a subclass of Animal
class. By using this Dog
class can inherit properties like species
and methods like eat()
, walk()
from the Animal
class. Then we can add more properties or methods(like bark()
) to the Dog
class.public class Bit2Byte {
static class Animal{
protected String species;
public Animal(String species) {
this.species = species;
}
public void eat(){
System.out.println("Animal is eating");
}
public void walk(){
System.out.println("Animal is walking");
}
}
static class Dog extends Animal{
public Dog(String species) {
super(species);
}
public void bark(){
System.out.println(species+" Dog is barking");
}
}
public static void main(String[] args) {
Dog dog = new Dog("Canine");
dog.eat(); // Animal is eating
dog.bark(); // Canine Dog is barking
}
}
int add(int, int)
and int add(int,int,int)
are different forms of same method add
.add
, and appropriate method will be called.public class Bit2Byte {
private static int add(int a, int b) {
return a + b;
}
private static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
int res = add(1,2);
System.out.println(res); // 3
res = add(1,2,3);
System.out.println(res); // 6
}
}
That's the end. We will be discussing each topic in details in later sections.
There is no shortcut other than practicing.
This is a comment 1.
This is a comment 2.