OOP's is a programming paradigm. A programming paradigm is an approach or methodology to solve a problem using a programming language.
There are different types of programming paradigms
Object-oriented paradigm
procedural paradigm
functional paradigm
logical paradigm
structural paradigm
Smalltalk, Java, C#, C++, Python etc.. are a few languages that have object-oriented programming.
There are 6 main pillars for OOPs
class
objects and methods
inheritance
polymorphism
abstraction
encapsulation
Consider a class of animals. It comprises many varieties like dogs, cats etc.. that perform activities like walking, running etc..
The above example is given as:
class=animals
objects=dog,cat
method=eat,run
Class
Class is a collection of objects
Class is not a real-world entity. It is a template/blueprint/prototype
Class does not occupy memory
//syntax of class
access-modifier class ClassName
{
//methods,constructors,fields,blocks,nested class
}
Object
Object is an instance of a class
Object is a real-world entity
Object occupies memory
Object consists of identity, state and behavior
identity - name
state/attribute - color, breed, age etc..
Behavior - eat, run etc..
Object can be created using the following:
new keyword
newInstance() method
clone() method
deserialization
factory methods
Methods
Method is a set of codes that perform a particular task.
Advantages of methods:
code reusability
code optimization
//syntax of method
access-modifier return-type methodName(list of parameters)
{
}
Declaration Instantiation Initialization
There are three main steps in object-oriented programming
Declaration
Instantiation
initialization
Declaration
It is declaring a variable name with an object type
For example, if want to declare animal buzo in our code we do it as
Animal buzo; //null is assigned to the memory
//animal=class name
//buzo = reference variable name
All the objects share the attributes and behavior of the class
Instantiation
This is when the memory is allocated for the object
The 'new' keyword is used to create the object
Animal buzo;
buzo = new;
Initialization
The "new" keyword is followed by a call to a constructor. This call initializes the new object
Animal buzo;
buzo = new Animal();
In initialization, the values are put into the memory that was allocated by the new keyword. The state and behavior of an object are loaded into the memory
Example :
breed, age, color = state/attributes
eat, run, bark = behavior
All the 3 steps are together written as
Animal buzo = new Animal(); //creating an object
buzo.run(); //calling a method
Simple JAVA program using OOP's
class Animal
{
public void run()
{
System.out.println("I am running");
}
public void eat()
{
System.out.println("I am eating");
}
public static void main(String[] args)
{
System.out.println("1");
}
}
In the above program, only 1 is printed as we did not create any objects to execute the above methods.
class Animal
{
public void run()
{
System.out.println("I am running");
}
public void eat()
{
System.out.println("I am eating");
}
public static void main(String[] args)
{
System.out.println("1");
Animal buzo = new Animal();
buzo.eat();
buzo.run();
}
}
Any number of objects can be created in a single class
class Animal
{
public void run()
{
System.out.println("I am running");
}
public void eat()
{
System.out.println("I am eating");
}
public static void main(String[] args)
{
System.out.println("1");
Animal buzo = new Animal();
buzo.eat();
buzo.run();
Animal bruno = new Animal();
bruno.eat();
bruno.run();
}
}
We can create any number of classes but all the classes are initialized in the main method only
class Animal
{
public void run()
{
System.out.println("I am running");
}
public void eat()
{
System.out.println("I am eating");
}
public static void main(String[] args)
{
System.out.println("1");
Animal buzo = new Animal();
buzo.eat();
buzo.run();
buzo.fly();
}
}
class Birds
{
public void fly()
{
System.out.println("I am flying");
}
}
The above code generates an error as the method "fly" is declared in the birds class but the object "buzo" is of class animals.
class Animal
{
public void run()
{
System.out.println("I am running");
}
public void eat()
{
System.out.println("I am eating");
}
}
class Bird
{
public void fly()
{
System.out.println("I am flying");
}
public static void main(String[] args)
{
System.out.println("1");
Animal buzo = new Animal();
buzo.eat();
buzo.run();
Bird sp = new Bird();
sp.fly();
}
}
Above code contains various classes and methods initialized in the main method