What is OOP? OOP is a programming paradigm What is programming paradigm A method or way to write a computer program for real world applications. Is there any other programming paradigm exists Yes Which one? Structure programming Functional programming Procedural programming Logical programming
6 Main pillars/concepts of OOP programming paradigm Classes Objects & methods Inheritance Polymorphism Abstraction Encapsulation Mastering these concepts means you are going to become an expert programmer in industry soon.
How to understand these concepts? To understand OOP concepts relate them with real world. Whatever you do in your real world is actually one of the OOP concept.
Which languages are OOP language? Smalltalk is first and purely OOP language. JAVA, C#, C++, Python etc these all are OOP languages.
Class In real world class means a presentation for the group of some related objects like Animals class Birds class Vehicles class Human class, etc. For example if we talk about animal class than which objects should fall in this class Dog Cat Horse Tiger, etc.
class For example in bird class objects should be Sparrow Peacock Parrot Owl, etc. A class represents some methods/activities that the objects of that class follow, for example Animals can (eat, run, walk, drink, fight, etc ) Birds can (fly, eat, walk, drink, etc ) Humans can (eat, run, walk, drink, fight, read, etc )
Definition of class? Class is the collection of objects. Class is not the real world entity, it is just the template or blueprint.* Class does not occupy memory.**
Syntax of class: access-modifiers class ClassName { Class body (methods, constructors, fields, blocks, nested class); } In case of no access-modifier will written the default access modifier will be considered.
Methods Method is a set of code which perform a particular task. Example eat(), run(), walk(), etc. Advantages of methods: Code reusability Code Optimization
Syntax of method access-modifier return-type methodName (list of parameters) { Method body(conditional, switch, control statements, variables etc ) } If no access-modifier place the default modifier will take place in java.
Definition of Object Object is an instance of class e.g. dog is an instance of animal. Object is real world entity. Object occupies memory.
Object consists of Identity: name State/Attribute: Breed, Age, Color Behaviors: Eat(), Run(), Bark()
How to create an object in java There are five ways to create object in java By using new keywords By using newInstance () method By using clone() method By using deserialization Mainly objects created by using new keyword.
Steps to create object using new keyword. Declaration Declare variable that represent the object name. For example Animal buzo ; Instantiation and Initialization (Initialization perform by using class name constructor. ) buzo = new Animal(); By combining declaration, instantiation and initialization the object can be create using following syntax. Animal buzo = new Animal();
How to call the behavior of object? The behavior/method of object call by using dot (.) operator. For example buzo.run () buzo.eat () How to change the state/attribute of an object? The state/attribute of object can also be change by using dot(.) operator. For example buzo.color = black; buzo.age =3;
Actual program for implementation public class Animal { Int age = 1; String breed = “GS”; public void eat() { System.out.println (“I am eating”); } public void run() { System.out.println (“I am running”); } } public static void main ( String args[]) { Animal dog = new Animal(); System.out.println ( dog.age ); System.out.println ( dog.breed ); dog.run (); dog.eat (); }