Covariant return types
Click to see answer
Covariant return types allow a method to return a subclass type in place of its superclass type.
Click to see question
Covariant return types
Covariant return types allow a method to return a subclass type in place of its superclass type.
Method overriding
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
Encapsulation is used to protect the internal state of objects and to control how other objects can access or modify that state.
Interfaces
Specify contracts that classes must adhere to, defining methods without implementation.
What is Abstraction?
Abstraction: Focus: What the object does, hiding implementation details. Goal: Simplifying complex systems by exposing only essential features. Mechanisms: Abstract classes, interfaces, functions.
Static blocks
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.
Interfaces
Can define abstract methods with common behavior that different classes can implement in their own way.
Inheritance
Inheritance is used to create a hierarchy of classes where subclasses inherit properties and methods from their parent classes.
Method Overriding
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
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.
Static variables
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.
What is Encapsulation?
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
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
Composition is used to build complex objects by combining simpler objects.
What is abstraction?
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 in Java
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
Covariant return types allow a method to return a subclass type in place of its superclass type.
Marker Interface
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.
SQLException
Not in the same class hierarchy as IOException.
Encapsulation
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.
Controlling Instantiation
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.
Abstract Class
Partial abstraction, shared implementation
What is Polymorphism in Java?
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.
What is the difference between static variable and an instance variable?
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.
Method overriding
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.
Abstract classes
Define a blueprint for subclasses with shared functionality and abstract methods that must be implemented.
Public Access Modifier
Public variables and methods can be accessed from anywhere in the program.
Exception
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
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.
What are the advantages of abstraction?
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.
What does Static keyword signify?
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
FileNotFoundException is a subclass of IOException and is thrown when a file specified by the application is not accessible or does not exist.
Initializing Member Variables
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.
Calling Superclass Constructors
Subclasses must call their superclass constructor (implicitly or explicitly) during their own construction to ensure proper initialization of inherited state.
What is Abstraction in Java?
Abstraction in Java is the process of hiding the implementation details and showing only the functionality to the user.
Inheritance
Supports multiple inheritance
Liskov Substitution Principle (LSP)
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.
Instantiation
Cannot be instantiated directly
What is Composition?
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
Aggregation is used to represent relationships between objects without tightly coupling them together.
Abstraction
Abstraction is used to hide the implementation details of components and to present a simpler, higher-level interface to other parts of the system.
IOException
A more general exception than FileNotFoundException.
RuntimeException
An unchecked exception that can be thrown even if the overridden method does not throw any exceptions.
RuntimeException
RuntimeException is a subclass of Exception and represents exceptions that can be thrown during the normal operation of the Java Virtual Machine.
Shared Initialization Logic
Common initialization steps for all subclasses can be consolidated in the abstract class constructor, reducing code duplication.
What is the difference between Abstract Class and Interface in Java?
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.
Open/Closed Principle (OCP)
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.
What is Method Overloading and Overriding in Java?
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 to Use: Abstract classes
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
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.
What is Aggregation?
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
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
IOException is a checked exception that can occur during input and output operations in Java.
Exception
Exception is a superclass of all checked exceptions in Java and is used to handle exceptional conditions that a program should catch.
Method overriding
Allows subclasses to provide their own implementation of a method inherited from a superclass.
Abstraction
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.
Methods
Can have abstract and non-abstract methods
User
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.
What is Composition in Java?
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.
Constructors
Cannot have constructors
Method Overriding
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 Access Modifier
Private variables and methods can only be accessed within the same class.
Static methods
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.
Method overloading
Allows a class to define multiple methods with the same name but different parameter types or numbers.
Enforcing Invariants and Constraints
Constructors in abstract classes can enforce rules and constraints that must hold true for all objects in the hierarchy, ensuring data integrity and validity.
What are SOLID principles, with example?
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
What are the benefits of Abstraction in Java?
The benefits of Abstraction in Java include reduced complexity, improved reusability, and maintainability of the code.
Implementation
Subclass must implement all abstract methods
Coupling
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.
Constructor
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 Access Modifier
Protected variables and methods can be accessed within the same class, and by subclasses and classes in the same package.
Difference between Abstraction and Encapsulation
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
SQLException is a checked exception that is thrown when a database access error occurs or the SQL database access is interrupted.
Interface
Complete abstraction, contract
Methods
Traditionally only abstract methods (can have default and static methods in Java 8+)
What is the difference between Abstraction and Encapsulation in Java?
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.
Dependency Inversion Principle (DIP)
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.
Main method
Cannot have a main method
Access Modifiers in Java
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.
What is method overloading in Java?
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
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.
What is a Constructor in Abstract Class?
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.
What is Inheritance in Java?
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.
Cohesion
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.
Can we override the static method? Why Can't we do that?
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.
Inheritance
Supports single inheritance
Variables
Only static final variables
Instantiation
Cannot be instantiated directly
When to Use: Interfaces
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.
Implementation
Implementing class must provide code for all methods
Main method
Can have a main method
What is Covariant type?
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
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.
What is Inheritance?
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.
FileNotFoundException
A subclass of IOException.
Functions
Hide the internal logic of a specific task, providing a simple interface for other parts of the program to interact with.
Polymorphism
Enables an object to exhibit different behaviors depending on its actual type at runtime.
Exception Rules for Overriding
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.
What is the difference between Abstraction and polymorphism?
Abstraction: Focus: Hides the internal complexity of an object, exposing only the essential features and functionalities that users need to interact with.
SOLID Principles
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.
Variables
Can have static, non-static, final, non-final
Interface Segregation Principle (ISP)
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.
Constructors
Can have constructors
Inheritance
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.