[Audio] Definition Object Oriented Programming (O-O-P) is a programming approach in which software is organized and designed using objects and classes. This method allows developers to create reusable, secure, and easily maintainable programs. Main Features of O-O-P 1. Classes and Objects 2. Encapsulation 3. Inheritance 4. Polymorphism 5. Abstraction Methods: Declaration, parameters, return types, method overloading. Classes and Objects: Creating classes, instantiation, constructors, instance variables, methods. Encapsulation: Access modifiers (public, private, protected, default), getters and setters. Inheritance: Extending classes, super keyword, method overriding. Polymorphism: Method overloading, method overriding, dynamic method dispatch. Abstraction: Abstract classes, methods, interfaces. Methods Definition A method is a block of code that performs a specific task. It can be called and executed by an object or class. Syntax [Access Modifier] [Return Type] [Method Name] ([Parameters]) Explanation Access Modifier: Determines the accessibility of the method. It can be public, private, protected, or default. Return Type: Specifies the type of data that the method will return, if any. Method Name: A unique identifier for the method. Parameters: Values that are passed into the method for it to use. Method Body: The code that is executed when the method is called. Classes and Objects Definition A class is a blueprint or template for creating objects. An object is an instance of a class. Syntax public class ClassName // Methods [Access Modifier] [Return Type] [Method Name] ([Parameters]) } Explanation Instance Variables: Variables that belong to a specific object. They hold the object's data. Constructor: A special method that is used to initialize an object when it is created. The constructor must have the same name as the class. Methods: Behaviors or actions that an object can perform. Encapsulation Definition Encapsulation is the process of hiding the internal implementation details of an object from the outside world. Syntax [Access Modifier] [Data Type] [Variable Name]; Explanation Access Modifier: Determines the visibility of the instance variable. Private variables can only be accessed within the class, while public variables can be accessed from outside the class. Data Type: Specifies the type of data that the variable can hold. Variable Name: A unique identifier for the variable. Inheritance Definition Inheritance is a mechanism in which one class (the child class or subclass) inherits the properties and methods of another class (the parent class or superclass). Syntax public class ChildClass extends ParentClass Explanation ChildClass: The class that inherits from the parent class. ParentClass: The class that is being inherited from. Polymorphism Definition Polymorphism is the ability of an object to take on multiple forms. Syntax Method Overloading: Having multiple methods with the same name but different parameters within the same class. Method Overriding: Redefining a method in the child class that was already defined in the parent class. Dynamic Method Dispatch: The process of deciding which version of an overridden method will be called at runtime based on the actual type of the object. Abstraction Definition Abstraction is the process of hiding the implementation details of a class and only exposing the essential features to the user. Syntax Abstract class ClassName {.
[Audio] returnType methodName(parameters) Example class Demo public static void main(String[] args) } Explanation: void → return type display() → method name Method prints a message. Real Time Example: A-T-M Machine: withdraw() deposit() checkBalance() Each action is a method. Method Parameters: Definition: Parameters are values passed into methods..
[Audio] Example class Addition public static void main(String[] args) } Explanation: The above code shows a class named Addition with a method named main that takes in an array of strings as its parameter. Real Time Example: An example of this in real life could be a food delivery app, where the method placeOrder is used with parameters for the food name and quantity. Return Types: Return types refer to the type of value that a method will send back. Definition: In this example, the return type would be an integer for the method square, which takes in a parameter n and returns its squared value. Example: int square(int n) Explanation: This method would take in an integer value n and return its square value as an integer..
[Audio] The method gives back a whole number value. Real life Example The calculator application gives back the calculated result. Method overloading is the process of creating multiple methods with the same name but different parameters. Syntax: methodName (int a) methodName (int a, int b) Example: class MathOperation int add (int a, int b, int c) public static void main (String[] args) Explanation: This means that the class named MathOperation has a method called add that takes in three integer parameters (a, b, and c). In the main method, the program can then call the add method with different sets of parameters, such as add(a, b) or add(a, b, c), depending on the specific calculation needed. This allows for more flexibility and efficiency in coding..
[Audio] The add() method behaves differently depending on the parameters it receives. Real Time Example Mobile Camera: takePhoto() takePhoto(filter) takePhoto(filter, brightness) 2. Classes and Objects Class Definition A class is a template for creating objects. Syntax class ClassName Object Definition An object is a specific instance of a class. Syntax ClassName obj = new ClassName(); Example class Car.
[Audio] void drive() means the method named 'drive' has no return type and takes no parameters. public static void main(String[] args) is the program's entry point, where execution begins. } Explanation: The first line defines a method called 'drive' which does not return any value and does not require any input to be executed. The second line contains the main method, which is the starting point of the program. It is declared as public (accessible from outside the class), static (can be called without creating an object), and void (does not return any value). It takes a String array as input, which can hold command line arguments. The closing curly bracket (}) signifies the end of the class definition. Car is a class, which can be seen as a blueprint or a template for creating objects. c is an object of the class Car. Objects have access to the variables and methods defined in their class. Real Time Example: In the context of a school, the class can be seen as a subject, such as math or science. The students (Surya, Ravi, Priya) are objects of that class, as they all belong to the subject and have access to the information and methods related to it. Constructor: Definition: A constructor is a special method that is automatically called when an object is created. It is responsible for initializing the object's instance variables. Syntax: ClassName() where ClassName is the same name as the class. Example: If we have a class called Car, its constructor would be written as Car(). This constructor would be called automatically when an object of the class Car is created, and it would initialize the object's variables..
[Audio] class Student } Explanation: A class named Student is declared, which includes a main method. This main method is where the execution of the program starts. Real Time Example: When a new Instagram account is created, the profile setup process happens automatically. Instance Variables: Definition: Variables that are declared within a class, but outside of any methods, are called instance variables. Example: class Employee Real Time Example: Bank account details are an example of instance variables, such as the account number and customer name..
[Audio] Balance 3. Encapsulation Definition Encapsulation means combining data and methods and keeping data hidden from external access. Advantages Security Data hiding Controlled access Access Modifiers Modifier Accessible Within Class Package Subclass Outside Package public Yes Yes Yes Yes private Yes No No No protected Yes Yes Yes No default Yes Yes No No Getters and Setters Example class Bank.
[Audio] balance = b; } public int getBalance() } class Main } Explanation The variable 'balance' is set to 'b'. The 'getBalance' method returns an integer value. The 'Main' class is used as an entry point. Data Hiding This is when access to data is limited. Real Time Example In U-P-I Apps, users are not able to directly change their account balance. Data Hiding Definition Restricting direct access to data. Example In this example, the 'password' variable is declared as private, preventing direct access from outside the class..
[Audio] Real Time Example: An example of inheritance in real time is how social media platforms keep user passwords hidden from public view. 4. Inheritance Definition: Inheritance is a programming concept that allows one class to inherit the properties and methods of another class. Syntax: The syntax for inheritance is class Child extends Parent. This indicates that the Child class is inheriting from the properties and methods of the Parent class. Example: An example of inheritance would be creating the Animal class and having the Dog class inherit from it. This would allow the Dog class to have access to the properties and methods of the Animal class. } class Animal class Dog extends Animal }.
[Audio] Explanation: A dog inherits features from the Animal class. Real Time Example: A child inherits characteristics from their parents. Super Keyword: A keyword that refers to the parent class object. Example: class Parent class Child extends Parent } Explanation: The super() method calls the parent constructor..
[Audio] Method Overriding Definition: Subclass provides its own implementation of a parent method. Example: class Vehicle class Bike extends Vehicle } Explanation: The child method overrides the parent method. Real Time Example: Different payment apps process payments in various ways. 5. Polymorphism Definition: Polymorphism means many forms..
[Audio] Types 1. Compile time polymorphism → Method overloading 2. Runtime polymorphism → Method overriding Dynamic Method Dispatch Definition Parent reference points to child object. Example: Class Animal{} Class Cat extends Animal{} Class Main{} Explanation: In Java, polymorphism is achieved through two main types compile time polymorphism and runtime polymorphism. Method overloading is a type of compile time polymorphism where multiple methods with the same name but different parameters are created in a class. Method overriding, on the other hand, is a type of runtime polymorphism where a child class overrides a method of its parent class. Dynamic Method Dispatch is the process of determining which method to call at runtime, based on the actual object being referred to by a parent class reference. In the given example, the Main class creates an object of type Cat, which extends the Animal class. The parent class reference (Animal) is used to point to the child object (Cat) during runtime..
[Audio] A method call is determined at runtime. Real life example: Google Maps. There are different modes such as car mode, bike mode, and walking mode, where the same function may behave differently. 6. Abstraction: Definition: Abstraction hides the details of how something is implemented and only shows the important aspects. Abstract Class: Definition: A class that is declared with the keyword . Example: class Shape class Circle extends Shape public static void main(String[] args).
[Audio] c.draw(); } } Explanation An class contains methods. Abstract method Definition A method without a body. Syntax void methodName(); Interface Definition An interface contains methods used for complete ion. Example interface Animal Class Dog implements Animal Public static void main (String[] args).
[Audio] d.sound(); } } Explanation The class is implementing methods from an interface. Real Time Example Remote Control: The buttons are visible on the surface, but the internal wiring is hidden. Final Conclusion Object Oriented Programming (O-O-P--) provides benefits such as: Reusability Security Ease of maintenance Real world representation Organized code structure It is commonly used in: Banking applications Mobile apps Games Web applications Software development.