▪ Object-Oriented Programming(OOP) in Java is a programming paradigm centered around the concept of objects, which are instances of classes. ▪ OOP is built around four key principles. ▪ Inheritance ▪ Abstract ▪ Encapsulation ▪ Polymorphism OOP in Java
▪ Inheritance isone of the useful feature of OOPs. It allows a class to inherit the properties and methods of another class. ▪ A class inheriting properties and methods of another class can use those without declaring them. ▪ The main purpose of inheritance in java is to provide the reusability of code. ▪ So that a class has to write only the unique features and rest of the common properties and functionalities can be inherited from another class. Inheritance
▪ A classcan only inherit the fields (or properties) and methods of another class, if it extends the class. ▪ Child Class: ▪ The class that extends the features of another class is known as child class, sub class or derived class. In the above code, class A is the child class. ▪ Parent Class: ▪ The class that shares the fields and methods with the child class is known as parent class, super class or Base class. In the above code, Class B is the parent class. Inheritance
7.
▪ Inheritance removesredundancy from the code. A class can reuse the fields and methods of parent class. No need to rewrite the same redundant code again. ▪ Inheritance allows us to reuse of code, it improves reusability in your java application. ▪ Reduces code size: By removing redundant code, it reduces the number of lines of the code. ▪ Improves logical structure: Improves the logical structure of the code that allows programmer to visualize the relationship between different classes. Advantages of using Inheritance
8.
Types of Inheritance •There are four types of inheritance in Java: • Single • Multilevel • Hierarchical • Hybrid
▪ Polymorphism inJava is one of the four fundamental principles of Object-Oriented Programming (OOP). ▪ The term "polymorphism" means "many forms," and in Java, it allows objects to take on multiple forms. ▪ Polymorphism enables a single interface to be used to represent different underlying forms (data types or methods). Polymorphism
▪ Reusability: Writecode that works with multiple types. ▪ Flexibility and Scalability: Handle new types with minimal changes to existing code. ▪ Reduced Coupling: Allows loosely coupled design. ▪ Improved Maintainability: Makes code easier to manage and extend. Advantages of Polymorphism
▪ It isthe process of grouping the data with its related methods into single unit(Class). ▪ To prevent unauthorized access to data members of object. ▪ In Java, encapsulation is primarily implemented by: ▪ Declaring fields (variables) as private. ▪ Providing public getter and setter methods to access and modify the fields. Encapsulation
22.
▪ Data Hiding:Prevents unauthorized access to sensitive data. ▪ Improved Security: Restricts unwanted changes to the fields. ▪ Flexibility: Allows changes in the implementation without affecting external code. ▪ Code Reusability: Promotes modular and maintainable code. Encapsulation
Abstraction • Abstraction inJava is one of the four pillars of Object-Oriented Programming (OOP). • It focuses on hiding the implementation details of a class and showing only the essential features (or behavior) to the user. • This helps to reduce complexity and increase the readability of the code. • Key concepts of abstraction: • Hiding Implementation: The user interacts with the high-level operations, without knowing the underlying complexities.
25.
Abstraction • Show OnlyWhat is Necessary: Expose only the methods that are needed and hide the rest. • Achieved By: • Abstract Classes • Interfaces
26.
Abstract Classes • Anabstract class is a class that is declared using the abstract keyword. • It can contain abstract methods (methods without a body) and concrete methods (methods with a body). • Characteristics of Abstract Classes: • Cannot be instantiated (you cannot create an object of an abstract class). • Can have both abstract and non-abstract methods. • Can have constructors, fields, and static methods.
Interfaces • An interfaceis a blueprint of a class that contains abstract methods. • In Java 8 and later, it can also contain default and static methods. • Characteristics of Interfaces: • All methods in an interface are implicitly abstract and public (prior to Java 8). • Cannot contain constructors or instance fields (only constants). • A class can implement multiple interfaces, supporting multiple inheritance.
Constructors • A constructorin Java is a special method used to initialize objects. • It is called when an object of a class is created. • A constructor has the same name as the class and does not have a return type, not even void. • Key Features of a Constructor • Name: Must be the same as the class name. • No Return Type: Does not have any return type. • Automatic Invocation: Called automatically when an object is created. • Can Be Overloaded: Multiple constructors can be defined in a class with different parameter lists.
31.
Constructors • Types ofConstructors in Java 1. Default Constructor: • No arguments. • Automatically provided by Java if no constructors are explicitly defined. 2. Parameterized Constructor: • Takes arguments to initialize the object with specific values.