Covariant return types allow a method to return a subclass type in place of its superclass type.
Method overriding is a feature in object-oriented programming that allows a subclass to provide a specific implementation of a method that is already provided by its superclass. This allows the subclass to customize the behavior of the method while maintaining the method's signature and return type.
Encapsulation is used to protect the internal state of objects and to control how other objects can access or modify that state.
Specify contracts that classes must adhere to, defining methods without implementation.
Abstraction: Focus: What the object does, hiding implementation details. Goal: Simplifying complex systems by exposing only essential features. Mechanisms: Abstract classes, interfaces, functions.
A static block is a block of code that is executed when the class is loaded. Static blocks are used to initialize static variables or to perform other one-time initialization tasks.
Can define abstract methods with common behavior that different classes can implement in their own way.
Inheritance is used to create a hierarchy of classes where subclasses inherit properties and methods from their parent classes.
The ability of a subclass to provide a specific implementation of a method that is already provided by its parent class. It allows a child class to provide a specific implementation of a method that is already provided by its parent class.
Method overloading is the ability of a class to have multiple methods with the same name but with different parameters. This allows for the creation of methods with the same name but different behavior based on the input parameters. It is also known as 'compile-time polymorphism' or 'function overloading' in some languages.
A static variable is a variable that belongs to the class and is shared by all instances of the class. Static variables are declared using the static keyword and are often used for constants or for variables that need to be shared across instances of the class.
Encapsulation: Focus: How the object's data and behavior are bundled together. Goal: Protecting data integrity and controlling access. Mechanisms: Access modifiers (public, private, protected), getters and setters.
Multilevel inheritance is a type of inheritance in object-oriented programming (OOP) where a derived class (subclass) is created from another derived class, which itself was derived from a base class (superclass). In multilevel inheritance, each derived class inherits the characteristics of the class above it in the hierarchy. This means that a subclass not only has all the features of its immediate superclass, but also those of all its ancestors up the hierarchy chain.
Composition is used to build complex objects by combining simpler objects.
Abstraction is the act of focusing on the essential details of something while hiding away the unnecessary complexity. In programming, it allows breaking down complex systems into smaller, easier-to-understand pieces by defining interfaces or classes that expose only the relevant functionalities, hiding the inner workings.
Access modifiers control the visibility of variables and methods in a class. There are three access modifiers in Java: public, private, and protected.
Covariant return types allow a method to return a subclass type in place of its superclass type.
An interface without any methods is called a 'marker interface'. It is used to mark a class as having some specific behavior or property, without specifying any methods for that behavior or property.
Not in the same class hierarchy as IOException.
This refers to the practice of hiding the internal workings of an object and exposing only the necessary functionality. The data and behaviour of an object are encapsulated within the object, and can only be accessed through well-defined interfaces.
Constructors in abstract classes can be made private or protected to control how subclasses are created, ensuring they are instantiated through specific mechanisms or helper methods.
Partial abstraction, shared implementation
Polymorphism in Java allows an object to take on many forms. It is the ability of a method to do different things based on the object that it is acting upon.
Static variables are declared using the "static" keyword and are shared across all instances of a class. They are initialized only once, when the class is loaded, and retain their value throughout the execution of the program. Instance variables, on the other hand, are declared without the "static" keyword and are unique to each instance of a class. They are initialized when an object is created and are destroyed when the object is destroyed.
In Java, method overriding is when a subclass provides its own implementation of a method that is already defined in its superclass. The subclass method must have the same name, return type, and parameter list as the superclass method. Access specifiers determine the visibility of a method, and they can also be used when overriding methods. When overriding a method, the access specifier of the overriding method cannot be more restrictive than the access specifier of the overridden method. In other words, if the overridden method is public, the overriding method must also be public or less restrictive.
Define a blueprint for subclasses with shared functionality and abstract methods that must be implemented.
Public variables and methods can be accessed from anywhere in the program.
An event that disrupts the normal flow of the program's instructions. It is an object that wraps an error event and contains information about the error, such as its type and the state of the program when the error occurred.
Abstraction is used to hide the implementation details of a class or component and to present a simpler, higher-level interface to other parts of the system. It is typically used in system design and architecture, where we define components and their interfaces.
The advantages of abstraction include reduced complexity, increased productivity, improved reusability, and enhanced readability. It makes code easier to understand, write, and maintain by breaking down large problems into smaller, more manageable chunks, allowing focus on logic and functionality without getting bogged down in implementation details, promoting modularity and reducing code duplication, and making code more concise and less cluttered for better understanding and collaboration.
In Java, the static keyword is used to create variables, methods, and blocks that belong to the class, rather than to an instance of the class.
FileNotFoundException is a subclass of IOException and is thrown when a file specified by the application is not accessible or does not exist.
Abstract classes often have member variables that need to be initialized for proper object state. Constructors perform this initialization, ensuring consistent setup for all subclasses.
Subclasses must call their superclass constructor (implicitly or explicitly) during their own construction to ensure proper initialization of inherited state.
Abstraction in Java is the process of hiding the implementation details and showing only the functionality to the user.
Supports multiple inheritance
This principle states that derived classes should be able to replace their base classes without affecting the correctness of the program. In other words, if we have a base class Animal and a derived class Dog, we should be able to use the Dog class wherever we use the Animal class. For example, if we have a method that takes an Animal parameter and performs some action, we should be able to pass a Dog object to that method without any issues.
Cannot be instantiated directly
Composition is a strong type of association where an object is made up of one or more objects of other classes. For example, a car is composed of various parts such as wheels, engine, transmission, etc. The car class has an object of the wheel class, engine class, and transmission class as its member variables.
Aggregation is used to represent relationships between objects without tightly coupling them together.
Abstraction is used to hide the implementation details of components and to present a simpler, higher-level interface to other parts of the system.
A more general exception than FileNotFoundException.
An unchecked exception that can be thrown even if the overridden method does not throw any exceptions.
RuntimeException is a subclass of Exception and represents exceptions that can be thrown during the normal operation of the Java Virtual Machine.
Common initialization steps for all subclasses can be consolidated in the abstract class constructor, reducing code duplication.
An abstract class can have both abstract and concrete methods while an interface can only have abstract methods. A class can implement multiple interfaces but can extend only one abstract class.
This principle states that a class should be open for extension but closed for modification. This means that we should be able to add new functionalities or behaviors to a class without changing its existing code. For example, instead of modifying an existing Payment class to add support for a new payment method, we can create a new PaymentMethod class that implements a Payment interface and inject it into the Payment class.
Method overloading is the ability to define multiple methods with the same name but different parameters in a class. Method overriding is the ability of a subclass to provide a specific implementation of a method that is already provided by its parent class.
When you have some common implementation to share among subclasses. When you want to enforce a hierarchy and prevent direct instantiation of the base class. When you need to control access to members using access modifiers.
Method overloading is when a class has multiple methods with the same name but different parameters. The methods must have different parameter types or a different number of parameters.
Aggregation is a weak type of association where an object contains a reference to one or more objects of other classes. For example, a university class has a collection of student classes as its member variable. The student class has an object of the university class as its member variable.
Method overriding is the ability of a subclass to provide a different implementation of a method that is already provided by its superclass. It involves defining a method in the subclass with the same name, return type, and parameters as the method in the superclass. This allows for changing the behavior of the method in the subclass. It is also known as 'runtime polymorphism' or 'function overriding'.
IOException is a checked exception that can occur during input and output operations in Java.
Exception is a superclass of all checked exceptions in Java and is used to handle exceptional conditions that a program should catch.
Allows subclasses to provide their own implementation of a method inherited from a superclass.
Abstraction refers to the process of identifying common patterns and extracting essential features of objects, creating classes from these patterns. Abstraction allows for the creation of higher-level concepts that can be used in multiple contexts, and can simplify complex systems.
Can have abstract and non-abstract methods
It should only be responsible for handling user-related functionalities such as authentication, user data management, etc. It should not be responsible for other unrelated functionalities like sending emails or managing payment transactions.
Composition in Java allows a class to be composed of other objects. This means that a class can have references to other objects as its properties and use them to delegate tasks or behaviors.
Cannot have constructors
Method overriding is when a subclass provides its own implementation of a method that is already defined in its superclass. The subclass method must have the same name, return type, and parameter list as the superclass method.
Private variables and methods can only be accessed within the same class.
A static method is a method that belongs to the class and can be called without creating an instance of the class. Static methods are declared using the static keyword and are often used for utility methods that do not depend on the state of an instance.
Allows a class to define multiple methods with the same name but different parameter types or numbers.
Constructors in abstract classes can enforce rules and constraints that must hold true for all objects in the hierarchy, ensuring data integrity and validity.
SOLID is an acronym that represents five principles of object-oriented design that aim to make software more maintainable, flexible, and easy to understand. Here are the explanations of each of the five SOLID principles, along with examples: Single Responsibility Principle (SRP): This principle states that a class should have only one reason to change. In other words, a class should have only one responsibility or job. For example, if we have a class named
The benefits of Abstraction in Java include reduced complexity, improved reusability, and maintainability of the code.
Subclass must implement all abstract methods
Refers to the degree of interdependence between modules or components. High coupling means that a change in one module or component will likely affect other modules or components.
A constructor in an abstract class is used for initializing member variables, enforcing invariants and constraints, consolidating shared initialization logic, controlling instantiation, and calling superclass constructors for proper initialization of inherited state.
Protected variables and methods can be accessed within the same class, and by subclasses and classes in the same package.
Scope: Abstraction operates at a higher level, focusing on the overall design and interface. Encapsulation works at the object level, managing internal data and implementation. Purpose: Abstraction aims to simplify complexity and promote reusability. Encapsulation aims to protect data and manage dependencies. Implementation: Abstraction is often achieved through abstract classes or interfaces. Encapsulation is typically implemented using access modifiers and methods to control access to data.
SQLException is a checked exception that is thrown when a database access error occurs or the SQL database access is interrupted.
Complete abstraction, contract
Traditionally only abstract methods (can have default and static methods in Java 8+)
Abstraction focuses on the outside view of an object while Encapsulation focuses on the inside view of an object. Abstraction is used for hiding the unwanted details while Encapsulation means hiding the code and data into a single unit to protect the data from the outside world.
This principle states that high-level modules should not depend on low-level modules. Instead, both should depend on abstractions. This means that we should rely on abstractions, rather than concrete implementations. For example, instead of depending on a specific database implementation in a class, we should depend on a database interface, which can be implemented by different databases. This allows for easier testing, maintenance, and scalability.
Cannot have a main method
public: The most permissive access level. protected: Allows access within the same package and from any subclass. default (no modifier): Has package-level access. private: The most restrictive access level.
Method overloading in Java allows a class to have more than one method having the same name if their parameter lists are different. It is determined at compile time based on the number and type of arguments passed.
Polymorphism refers to the ability of objects to take on many forms, and is achieved through the use of inheritance, overloading and overriding methods, and interfaces. Polymorphism allows for greater flexibility and reuse of code.
A constructor in an abstract class is used to initialize the data members of the abstract class. It is called when an instance of the abstract class is created.
Inheritance in Java allows a class (called a subclass) to inherit properties and behaviors from another class (called a superclass). The subclass can then add or modify these properties and behaviors as needed.
Refers to the degree to which the elements within a module or component work together to achieve a single, well-defined purpose. High cohesion means that the elements within a module are strongly related and work together towards a common goal, while low cohesion means that the elements are loosely related and may not have a clear purpose.
No, it is not possible to override a static method in Java. A static method is associated with the class and not with an object, and it can be called directly on the class, without the need of creating an instance of the class. When a subclass defines a static method with the same signature as a static method in the superclass, the subclass method is said to hide the superclass method. This is known as method hiding. The subclass method is not considered an override of the superclass method, it is considered a new method and it hides the superclass method, but it doesn't override it. In summary, since a static method is associated with the class, not an object and it is directly callable on the class, it is not possible to override a static method in Java, instead, it is hidden by subclass method with the same signature.
Supports single inheritance
Only static final variables
Cannot be instantiated directly
When you want to define a contract that multiple unrelated classes can implement. When you want to achieve loose coupling between classes. When you need to support multiple inheritance.
Implementing class must provide code for all methods
Can have a main method
Covariant type refers to the ability to use a subclass type in place of its superclass type. In other words, it allows a subclass to be used in place of its superclass. This feature is supported by some programming languages such as Java and C#.
Encapsulation is used to protect the internal state of an object and to control how other objects can access or modify that state. It is typically used in data modelling, where we define classes that represent real-world entities and their properties.
Inheritance is a relationship between classes where one class inherits properties and behaviors from another class. It allows a class to inherit the characteristics of another class.
A subclass of IOException.
Hide the internal logic of a specific task, providing a simple interface for other parts of the program to interact with.
Enables an object to exhibit different behaviors depending on its actual type at runtime.
When overriding a method in Java, there are rules regarding exceptions: The overriding method can throw the same exceptions as the overridden method, or any subset of those exceptions. The overriding method can also throw unchecked exceptions, even if the overridden method does not. The overriding method cannot throw checked exceptions that are not in the same class hierarchy as the exceptions thrown by the overridden method. If the overridden method does not throw any exceptions, the overriding method cannot throw checked exceptions.
Abstraction: Focus: Hides the internal complexity of an object, exposing only the essential features and functionalities that users need to interact with.
The SOLID principles are a set of five principles for designing object-oriented software: Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle.
Can have static, non-static, final, non-final
This principle states that a class should not be forced to implement interfaces it does not use. In other words, we should separate interfaces that are too large or general into smaller and more specific interfaces. For example, instead of having a single Payment interface that includes all payment methods, we can have separate interfaces like CreditCardPayment, PayPalPayment, etc.
Can have constructors
Inheritance allows objects to inherit properties and behaviours from other objects. Inheritance allows for the creation of hierarchical relationships between classes, with parent classes passing down their characteristics to their child classes.