CoreJava - FAQs - Part 1

 

Core Java - FAQs -  Part 1

Q: What is a static variable in Java?

A: A static variable in Java is a variable that belongs to the class itself, rather than to any individual instance of the class. Static variables are shared among all instances of the class and can be accessed using the class name rather than an instance variable.

Q: What is a static method in Java? 

A: A static method in Java is a method that belongs to the class itself, rather than to any individual instance of the class. Static methods can be called using the class name rather than an instance variable, and they cannot access non-static data members or methods.

Q: What is the difference between a public and a private variable in Java? 

A: A public variable in Java can be accessed and modified by any code that has access to the object, while a private variable can only be accessed and modified by code within the same class.

Q: What is encapsulation in Java? 

A: Encapsulation is the practice of hiding the internal details of an object and exposing only the necessary information and functionality through a well-defined interface. In Java, this is typically achieved through the use of access modifiers (public, private, protected) and getter and setter methods.

Q: Why is encapsulation important in object-oriented programming? 

A: Encapsulation is important in object-oriented programming because it helps to keep code organized, maintainable, and reusable. By hiding the internal details of an object, you can protect it from outside interference and ensure that changes to the object's implementation don't affect the code that uses it.

Q: What is a getter method in Java? 

A: A getter method in Java is a method that is used to retrieve the value of a private data member from an object. Getter methods are typically named with a prefix of "get" followed by the name of the data member they retrieve.

Q: What is a setter method in Java? 

A: A setter method in Java is a method that is used to modify the value of a private data member in an object. Setter methods are typically named with a prefix of "set" followed by the name of the data member they modify, and take a single parameter that specifies the new value.

Q: What is the difference between a public and a private method in Java? 

A: A public method in Java can be accessed and called by any code that has access to the object, while a private method can only be accessed and called by code within the same class. Private methods are typically used for implementation details that should not be exposed to outside code.

Q: What is inheritance in Java?

A: Inheritance is the practice of defining a new class (called a subclass) based on an existing class (called a superclass), and inheriting the data members and methods of the superclass into the subclass. Subclasses can then add their own data members and methods, or override the methods of the superclass.

Q: What is a superclass in Java?

 A: A superclass in Java is the class that is being inherited from in an inheritance relationship. Superclasses provide a set of data members and methods that can be reused and customized by their subclasses.

Q: What is method hiding in Java? 

A: Method hiding is the practice of defining a new method in a subclass that has the same name as a method in the superclass, effectively hiding the superclass method. This can be done by using the "static" keyword in the subclass method definition.

Q: What is polymorphism in Java? 

A: Polymorphism is the ability of objects to take on multiple forms, or the ability of a method to operate on objects of different classes. In Java, this is typically achieved through inheritance and method overriding.

Q: What is method overriding in Java?

A: Method overriding is the practice of defining a method in a subclass that has the same name, return type, and parameters as a method in the superclass, effectively replacing the superclass method in the subclass. This allows the subclass to provide its own implementation of the method.

Q: What is an abstract class in Java? 

A: An abstract class in Java is a class that cannot be instantiated and is intended to be subclassed. Abstract classes can define abstract methods (methods without a body) that must be implemented by their subclasses.

Q: What is an abstract method in Java? 

A: An abstract method in Java is a method that is declared but not defined in an abstract class. Abstract methods do not have a body and are intended to be overridden by their subclasses.

Q: Can you create an object of an abstract class in Java? 

A: No, you cannot create an object of an abstract class in Java. Abstract classes are intended to be subclassed, and their abstract methods must be implemented by their subclasses.

Q: What is the difference between an abstract class and an interface in Java?

A: An abstract class in Java can have both abstract and non-abstract methods, while an interface can only have abstract methods. An abstract class can also have data members, while an interface cannot. Additionally, a class can only extend one abstract class, but can implement multiple interfaces.

Q: When should you use an abstract class versus an interface in Java?

A: You should use an abstract class in Java when you want to provide a common implementation for a set of related classes, or when you want to define some common behavior that can be shared among subclasses. You should use an interface when you want to define a contract that must be implemented by a set of unrelated classes, or when you want to provide a way for objects of different classes to be treated uniformly.

Q: What is an interface in Java? 

A: An interface in Java is a collection of abstract methods that can be implemented by any class. Interfaces define a contract that classes must follow, and can be used to achieve polymorphism.

Q: What is the difference between an interface and a class in Java? 

A: In Java, a class is a blueprint for creating objects, while an interface is a collection of abstract methods that can be implemented by any class. Classes can have instance variables and concrete methods, while interfaces cannot.

Q: Can a class implement multiple interfaces in Java? 

A: Yes, a class can implement multiple interfaces in Java. This allows the class to inherit the abstract methods and constants of multiple interfaces.

Q: What is the purpose of default methods in Java interfaces? 

A: Default methods were introduced in Java 8 to provide a way to add methods to an interface without breaking existing implementations. Default methods have a default implementation, but can be overridden by implementing classes.

Q: What is the purpose of static methods in Java interfaces? 

A: Static methods in Java interfaces provide a way to define utility methods that can be called without an instance of the interface. They are typically used for helper methods or factory methods.

Q: What is the difference between a class and an object in Java? 

A: A class is a blueprint for creating objects, while an object is an instance of a class. You can create many objects from a single class, and each object has its own set of instance variables and methods.

Q: What is the difference between a public and private method in Java? 

A: A public method in Java can be accessed by any other class, while a private method can only be accessed within the same class. Private methods are typically used for helper methods that are not meant to be called outside the class.

Q: What is the purpose of the "this" keyword in Java? 

A: The "this" keyword in Java refers to the current object instance. It can be used to access instance variables and methods within the current object, and to differentiate between instance variables and local variables that have the same name.

Q: What is a constructor in Java? 

A: A constructor in Java is a special method that is used to create and initialize objects of a class. Constructors have the same name as the class and do not have a return type.

Q: What is the difference between a default constructor and a parameterized constructor in Java?

A: A default constructor in Java is a constructor that takes no arguments, while a parameterized constructor is a constructor that takes one or more arguments. The purpose of a default constructor is to initialize instance variables with default values, while a parameterized constructor allows you to set initial values for instance variables when an object is created.

Q: What is encapsulation in Java? 

A: Encapsulation in Java is the practice of hiding implementation details of a class and exposing only a public interface to the outside world. This helps to prevent unintended modifications to the internal state of an object.

Q: What is the difference between encapsulation and abstraction in Java? 

A: Encapsulation in Java is the practice of hiding implementation details of a class, while abstraction is the practice of representing complex systems with simplified models. Encapsulation is a technique used to achieve abstraction.

Q: What is the purpose of getter and setter methods in Java?

A: Getter and setter methods in Java are used to access and modify the private instance variables of a class, respectively. They provide a way to encapsulate the internal state of an object and enforce data validation.

Q: What is data hiding in Java? 

A: Data hiding in Java is the practice of making instance variables private and providing public accessor methods (such as getters and setters) to access and modify them. This helps to prevent unintended modifications to the internal state of an object.

Q: How does encapsulation improve code quality in Java? 

A: Encapsulation in Java improves code quality by reducing the amount of code that needs to be changed when implementation details of a class are modified. It also helps to prevent unintended modifications to the internal state of an object and makes it easier to enforce data validation rules.

Q: What is inheritance in Java? 

A: Inheritance in Java is the process of creating a new class that is a modified version of an existing class. The new class inherits all the instance variables and methods of the existing class, and can add new variables and methods or modify existing ones.

Q: What is the difference between inheritance and composition in Java? 

A: Inheritance in Java is a mechanism for creating a new class that is a modified version of an existing class, while composition is the practice of creating a class that contains instances of other classes as instance variables. Inheritance is an "is-a" relationship, while composition is a "has-a" relationship.

Q: What is method overloading in Java? A

A: Method overloading in Java is the practice of creating multiple methods with the same name but different parameters. The compiler determines which version of the method to call based on the number and types of arguments passed.

Q: What is the difference between method overloading and method overriding in Java? 

A: Method overloading in Java is the practice of creating multiple methods with the same name but different parameters, while method overriding is the practice of creating a new implementation for an existing method in a subclass. Method overloading occurs at compile time, while method overriding occurs at runtime.

Q: What is polymorphism in Java? 

A: Polymorphism in Java is the ability of an object to take on multiple forms. In Java, this typically refers to the ability of a subclass object to be treated as an object of its superclass.

Q: What is dynamic polymorphism in Java? 

A: Dynamic polymorphism in Java is the ability of a program to determine at runtime which version of an overridden method to call, based on the actual type of the object. This is also known as late binding or runtime binding.

Q: What is static polymorphism in Java? 

A: Static polymorphism in Java is the ability of a program to determine at compile time which version of a method to call, based on the number and types of arguments passed. This is also known as early binding or compile-time binding.

Q: What is an abstract class in Java? 

A: An abstract class in Java is a class that cannot be instantiated on its own, but is meant to be subclassed. It may contain abstract methods, concrete methods, and instance variables.

Q: What is the difference between an abstract class and an interface in Java? 

A: An abstract class in Java is a class that cannot be instantiated on its own, but is meant to be subclassed. It may contain both abstract and concrete methods, and may have instance variables. An interface in Java is a collection of abstract methods and constants that can be implemented by any class.

Q: What is an abstract method in Java? 

A: An abstract method in Java is a method that is declared in an abstract class, but does not have an implementation. It must be implemented by any non-abstract subclass.

Q: What is the purpose of an abstract class in Java?

 A: The purpose of an abstract class in Java is to provide a common interface and implementation for a group of related classes. It can define common methods and instance variables, and enforce implementation of specific methods by subclasses.

Q: Can an abstract class have constructors in Java? 

A: Yes, an abstract class in Java can have constructors, just like any other class. However, an abstract class cannot be instantiated on its own, so its constructors are typically called by constructors of its subclasses.

Q: What is an interface in Java? 

A: An interface in Java is a collection of abstract methods and constants that can be implemented by any class. It defines a contract that a class must follow if it implements the interface.

Q: What is the difference between an abstract class and an interface in Java? 

A: An abstract class in Java is a class that cannot be instantiated on its own, but is meant to be subclassed. It may contain both abstract and concrete methods, and may have instance variables. An interface in Java is a collection of abstract methods and constants that can be implemented by any class.

Q: How do you implement an interface in Java? 

A: To implement an interface in Java, a class must include a declaration that it implements the interface, and provide implementations for all of the methods defined in the interface.

Q: Can a class implement multiple interfaces in Java? 

A: Yes, a class in Java can implement multiple interfaces. 

Q: What is the difference between a class and an object in Java? 

A: In Java, a class is a blueprint or template for creating objects, while an object is an instance of a class.

Q: What is a constructor in Java? 

A: A constructor in Java is a special method that is used to create and initialize an object of a class. It is called when an object is created using the "new" keyword.

Q: What is the default constructor in Java? 

A: The default constructor in Java is a constructor that is provided by the compiler if no other constructors are defined in a class. It has no arguments and does nothing.

Q: What is the difference between a parameterized constructor and a default constructor in Java? 

A: A parameterized constructor in Java is a constructor that takes one or more arguments, while a default constructor is provided by the compiler if no other constructors are defined in a class and has no arguments.

Q: What is a static method in Java? 

A: A static method in Java is a method that belongs to a class rather than an instance of a class. It can be called without creating an object of the class.

Q: What is encapsulation in Java? 

A: Encapsulation in Java is the practice of hiding the internal implementation details of a class from the outside world, and only exposing a public interface for interacting with the class.

Q: What is the difference between public, private, and protected access modifiers in Java? 

A: In Java, public methods and variables can be accessed from anywhere, private methods and variables can only be accessed within the same class, and protected methods and variables can be accessed within the same package or by subclasses.

Q: What is a getter method in Java? 

A: A getter method in Java is a method that is used to retrieve the value of a private variable in a class. It is typically a public method that returns the value of the variable.

Q: What is a setter method in Java? 

A: A setter method in Java is a method that is used to set the value of a private variable in a class. It is typically a public method that takes a parameter and sets the value of the variable.

Q: What is the purpose of encapsulation in Java? 

A: The purpose of encapsulation in Java is to provide a way to control access to the internal details of a class, to prevent unintended changes to the class from outside code, and to make the code easier to maintain and modify.

Q: What is inheritance in Java? 

A: Inheritance in Java is the practice of creating a new class from an existing class, with the new class inheriting the properties and methods of the existing class.

Q: What is the difference between a superclass and a subclass in Java? 

A: In Java, a superclass is the class from which a subclass is derived, while a subclass is the class that is derived from the superclass.

Q: What is a final class in Java?

 A: A final class in Java is a class that cannot be subclassed. Any attempts to create a subclass of a final class will result in a compile-time error.

Q: What is a final method in Java? 

A: A final method in Java is a method that cannot be overridden by any subclasses. It is often used to prevent a subclass from changing the behavior of a method in the superclass.

Q: What is an abstract class in Java?

 A: An abstract class in Java is a class that cannot be instantiated on its own, but is meant to be subclassed. It may contain abstract methods, concrete methods, and instance variables

Q: What is an abstract method in Java?

 A: An abstract method in Java is a method that has no implementation in the superclass, but is meant to be overridden by subclasses. It is declared using the "abstract" keyword.

Q: What is the difference between an abstract class and an interface in Java? 

A: In Java, an abstract class can have both abstract and concrete methods, and can have instance variables, while an interface can only have abstract methods and constants. A class can only inherit from one abstract class, but can implement multiple interfaces.

Q: Can an abstract class have a constructor in Java? 

A: Yes, an abstract class can have a constructor in Java. It can be used to initialize instance variables and perform other tasks when an object of the class is created.

Q: What is the purpose of an abstract class in Java?

 A: The purpose of an abstract class in Java is to provide a common interface for a group of related classes, while allowing each subclass to implement its own behavior. It is also used to enforce a particular structure or design pattern in a program.

Q: What is the difference between an abstract class and a concrete class in Java? 

A: In Java, an abstract class cannot be instantiated on its own, but is meant to be subclassed, while a concrete class can be instantiated and used directly. An abstract class can have abstract methods, while a concrete class must implement all of its methods.

Q: What is an interface in Java?

 A: An interface in Java is a collection of abstract methods and constants that define a contract for classes that implement the interface. It is used to define a common behavior or capability that can be shared by different classes.

Q: What is the difference between a class and an interface in Java?

 A: In Java, a class defines a blueprint for creating objects, while an interface defines a set of methods and constants that a class can implement. A class can implement multiple interfaces, but can only inherit from one class.

Q: What is the purpose of an interface in Java? 

A: The purpose of an interface in Java is to provide a common contract for a group of related classes, allowing them to be used interchangeably in certain contexts. It is also used to enforce a particular structure or design pattern in a program.

Q: Can an interface have instance variables in Java? 

A: No, an interface cannot have instance variables in Java. It can only have constants, which are declared using the "final" keyword.

Q: What is a default method in Java? 

A: A default method in Java is a method that has a default implementation in an interface, which can be used by classes that implement the interface. It is declared using the "default" keyword.

Q: What is the difference between a static method and an instance method in Java? A: In Java, a static method is a method that belongs to the class itself, while an instance method belongs to a specific object of the class. Static methods can be called using the class name, while instance methods are called using an object reference.

Q: What is a constructor in Java? 

A: A constructor in Java is a special method that is called when an object of the class is created. It is used to initialize instance variables and perform other tasks necessary to set up the object.

Q: What is method overriding in Java? 

A: Method overriding in Java is a mechanism by which a subclass can provide its own implementation for a method that is already defined in its superclass. The method in the subclass must have the same name, parameters, and return type as the method in the superclass.

Q: What is method hiding in Java? 

A: Method hiding in Java is a mechanism by which a subclass can define a static method with the same name as a static method in its superclass. The method in the subclass "hides" the method in the superclass, and can only be called using the subclass name.

Q: What is the difference between a public and a private variable in Java?

 A: In Java, a public variable can be accessed from anywhere, while a private variable can only be accessed within the class in which it is defined.

Q: What is encapsulation in Java? 

A: Encapsulation in Java is the practice of hiding the implementation details of a class from the outside world, and only exposing a public interface that can be used to interact with the class.

Q: What is the difference between encapsulation and abstraction in Java? 

A: In Java, encapsulation is the practice of hiding the implementation details of a class from the outside world, while abstraction is the practice of focusing on the essential features of a class and ignoring the implementation details.

Q: What are access modifiers in Java? 

A: Access modifiers in Java are keywords that determine the visibility of a class, method, or variable. The four access modifiers in Java are public, private, protected, and package-private.

Q: What is a getter method in Java? 

A: A getter method in Java is a method that is used to retrieve the value of a private instance variable. It is typically a public method that returns the value of the variable.

Q: What is a setter method in Java? 

A: A setter method in Java is a method that is used to set the value of a private instance variable. It is typically a public method that takes a parameter and sets the value of the variable.

Q: What is inheritance in Java? A: Inheritance in Java is the process by which a subclass inherits the properties and behavior of its superclass. The subclass can add its own properties and behavior, or override the properties and behavior of the superclass.

Q: What is the difference between inheritance and composition in Java? 

A: Inheritance in Java is the process by which a subclass inherits the properties and behavior of its superclass, while composition is the practice of creating a class that contains objects of other classes as instance variables.

Q: What is polymorphism in Java?

 A: Polymorphism in Java is the ability of an object to take on multiple forms. In Java, this is achieved through method overriding and method overloading.

Q: What is method overloading in Java? 

A: Method overloading in Java is the practice of defining multiple methods with the same name in a class, but with different parameters. Java determines which method to call based on the number and type of

Q: What is an interface in Java?

 A: An interface in Java is a collection of abstract methods that can be implemented by classes. An interface provides a contract of behaviour that a class must adhere to if it implements the interface.

Q: What is the difference between a class and an interface in Java?

 A: In Java, a class is a blueprint for creating objects, while an interface is a collection of abstract methods that can be implemented by classes. A class can extend another class, but can implement multiple interfaces.

Q: Can an interface have instance variables in Java? 

A: No, an interface in Java cannot have instance variables. It can only have constant variables that are declared as public, static, and final.

Q: What is the difference between implementing an interface and extending a class in Java? 

A: In Java, implementing an interface means providing an implementation for the abstract methods declared in the interface, while extending a class means inheriting the properties and behaviour of the superclass.

Q: Can a class implement multiple interfaces in Java? 

A: Yes, a class in Java can implement multiple interfaces. This allows the class to inherit the abstract methods declared in each interface and provide its own implementation for them.

Q: What is the difference between a class and an object in Java? 

A: A class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the properties and methods that objects of that class will have, while an object is a specific instance of a class that has its own state and behaviour.

Q: What is a static method in Java? 

A: A static method in Java is a method that belongs to a class and not to any specific object of that class. Static methods can be called without creating an object of the class, and they are often used for utility functions that don't depend on the state of any object.

Q: What is the difference between a public and a private method in Java? 

A: In Java, a public method is accessible from anywhere in the program, while a private method can only be accessed from within the class that defines it. This means that private methods are only visible to the class itself and not to any other classes.

Q: What is the difference between a public and a private variable in Java? 

A: In Java, a public variable is accessible from anywhere in the program, while a private variable can only be accessed from within the class that defines it. This means that private variables are only visible to the class itself and not to any other classes.

Q: What is the difference between a constructor and a method in Java? 

A: In Java, a constructor is a special method that is used to create objects of a class, while a method is a regular function that can be called on an object of a class to perform some action.

Q: What is encapsulation in Java? 

A: Encapsulation is a mechanism in Java that binds the data and methods that operate on the data into a single unit, and restricts access to the data from outside the class. This helps to maintain the integrity of the data and prevents unauthorized access.

Q: What is the access modifier in Java?

A: In Java, the access modifier is a keyword that determines the level of access to a class, method, or variable. The four access modifiers in Java are public, private, protected, and package-private.

Q: What is data hiding in Java? 

A: Data hiding is a technique in Java that involves encapsulating the data of a class and making it inaccessible from outside the class. This helps to protect the data from unauthorized access and ensures that it can only be modified through the methods of the class.

Q: What is the purpose of getters and setters in Java? 

A: Getters and setters are methods in Java that are used to access and modify the private variables of a class. Getters are used to retrieve the value of a variable, while setters are used to set the value of a variable.

Q: What is the difference between a public variable and a private variable in Java? 

A: In Java, a public variable can be accessed from anywhere in the program, while a private variable can only be accessed from within the class that defines it. This means that private variables are only visible to the class itself and not to any other classes.

Q: What is inheritance in Java? 

A: Inheritance is a mechanism in Java that allows a new class to be based on an existing class, inheriting the properties and behavior of the parent class. This allows for code reuse and makes it easier to maintain and update code.

Q: What is the difference between method overriding and method overloading in Java? 

A: Method overriding is a technique in Java that allows a subclass to provide its own implementation of a method that is already defined in the superclass, while method overloading is a technique that allows a class to have multiple methods with the same name but different parameters.

Q: What is the superclass in Java? 

A: The superclass in Java is the class that is being extended by another class, which is known as the subclass. The superclass provides the properties and methods that the subclass can inherit and modify.

Q: What is polymorphism in Java?

 A: Polymorphism is a mechanism in Java that allows objects of different classes to be treated as if they were objects of the same class. This allows for more flexible and dynamic programming.

Q: What is the difference between static and dynamic binding in Java? 

A: Static binding is a compile-time mechanism in Java that determines which method to call based on the type of the reference variable, while dynamic binding is a runtime mechanism that determines which method to call based on the actual type of the object.

Q: What is an abstract class in Java? 

A: An abstract class in Java is a class that cannot be instantiated, but is used as a superclass to other classes that can be instantiated. Abstract classes can contain abstract methods that must be implemented by their subclasses.

Q: What is an abstract method in Java? 

A: An abstract method in Java is a method that is declared but not implemented in an abstract class. Abstract methods must be implemented by the subclasses of the abstract class.

Q: What is the purpose of an abstract class in Java? 

A: The purpose of an abstract class in Java is to provide a common interface for a group of related classes. Abstract classes can define the common properties and methods that their subclasses will have, while allowing for variations in implementation.

Q: What is the difference between an abstract class and an interface in Java? 

A: An abstract class can contain both abstract and non-abstract methods, while an interface can only contain abstract methods. Additionally, a class can extend only one abstract class, but can implement multiple interfaces.

Q: Can an abstract class be final in Java? 

A: No, an abstract class cannot be final in Java, because a final class cannot be subclassed, and an abstract class is meant to be subclassed.

Q: What is an interface in Java? 

A: An interface in Java is a collection of abstract methods and constants that define a contract for the behavior of a class. An interface can be implemented by multiple classes to provide a common set of methods and properties.

Q: What is the difference between an interface and a class in Java? 

A: A class in Java can contain both properties and methods, while an interface can only contain abstract methods and constants. Additionally, a class can be instantiated to create objects, while an interface cannot be instantiated.

Q: Can a class implement multiple interfaces in Java? 

A: Yes, a class can implement multiple interfaces in Java, which allows it to provide implementations for the methods defined in all of the interfaces it implements.

Q: What is the purpose of default methods in Java interfaces?

 A: Default methods in Java interfaces provide a default implementation for a method, which can be overridden by the implementing classes if needed. This allows for greater flexibility in interface design.

Q: What is the difference between an abstract class and an interface in Java? 

A: An abstract class can contain both abstract and non-abstract methods, while an interface can only contain abstract methods and constants. Additionally, a class can extend only one abstract class, but can implement multiple interfaces.

Q: What is a constructor in Java? 

A: A constructor in Java is a special method that is used to initialize the instance variables of a class when an object is created. Constructors have the same name as the class and do not have a return type.

Q: What is the purpose of the "this" keyword in Java?

 A: The "this" keyword in Java is used to refer to the current object of a class. It can be used to access instance variables and methods of the current object, and to distinguish between instance variables and local variables with the same name.

Q: What is a package in Java?

 A: A package in Java is a collection of related classes and interfaces that provide a namespace for the classes. Packages are used to organize the code and to avoid naming conflicts with classes from other libraries.

Q: What is the difference between a public and private class in Java? 

A: A public class in Java can be accessed from other classes in the same package and from classes in other packages, while a private class can only be accessed from other classes in the same package.

Q: What is the difference between a static and non-static variable in Java? 

A: A static variable in Java is shared among all instances of a class, while a non-static variable is unique to each instance of the class. Static variables can be accessed using the class name, while non-static variables can only be accessed using an object reference.

Q: What is encapsulation in Java? 

A: Encapsulation in Java is the process of hiding the internal details of a class from the outside world and providing access to them through public methods. This helps to protect the data and behavior of the class and makes the code more modular.

Q: What is a getter method in Java? 

A: A getter method in Java is a public method that is used to access the value of a private instance variable of a class. Getter methods are often used to implement encapsulation.

Q: What is a setter method in Java? 

A: A setter method in Java is a public method that is used to set the value of a private instance variable of a class. Setter methods are often used to implement encapsulation.

Q: What is the difference between encapsulation and abstraction in Java? 

A: Encapsulation is the process of hiding the internal details of a class from the outside world, while abstraction is the process of hiding the implementation details of a class from the outside world. Encapsulation is achieved through access modifiers and getter/setter methods, while abstraction is achieved through abstract classes and interfaces.

Q: What is the purpose of access modifiers in Java? 

A: Access modifiers in Java are used to control the visibility and accessibility of classes, variables, and methods. They help to enforce encapsulation and prevent unauthorized access to the data and behavior of a class.

Q: What is the difference between composition and inheritance in Java? 

A: Composition is a design pattern in Java that involves creating objects of other classes within a class, while inheritance is a mechanism that allows a subclass to inherit the properties and methods of its superclass. Composition is often preferred over inheritance because it promotes code reuse and allows for more flexibility in design.

Q: What is the purpose of the "super" keyword in Java? A: The "super" keyword in Java is used to refer to the superclass of a class. It can be used to access the properties and methods of the superclass, to call the constructor of the superclass, and to override the methods of the superclass.

Q: What is method overriding in Java? 

A: Method overriding in Java is the process of redefining a method in a subclass that is already defined in its superclass. The overriding method must have the same name, the same return type, and the same parameters as the overridden method. When a method is called on an object of the subclass, the overridden method in the subclass is executed instead of the method in the superclass.

Q: What is dynamic method dispatch in Java? 

A: Dynamic method dispatch in Java is the mechanism by which the correct version of an overridden method is called at runtime. When a method is called on an object, the JVM determines the actual class of the object at runtime and calls the appropriate version of the method based on the actual class.

Q: What is the difference between method overloading and method overriding in Java? 

A: Method overloading in Java is the process of defining multiple methods with the same name in a class, but with different parameters. Method overriding is the process of redefining a method in a subclass that is already defined in its superclass. Method overloading is determined at compile-time, while method overriding is determined at runtime.

Q: What is an abstract class in Java? 

A: An abstract class in Java is a class that cannot be instantiated, but can be subclassed. Abstract classes are used to define a common behavior for a group of subclasses, and to provide default implementations for some of their methods.

Q: What is an abstract method in Java? 

A: An abstract method in Java is a method that is declared in an abstract class, but has no implementation. Abstract methods must be implemented by the subclasses of the abstract class.

Q: What is the difference between an abstract class and an interface in Java? 

A: An abstract class in Java can have both abstract and non-abstract methods, while an interface can only have abstract methods. An abstract class can also have instance variables, constructors, and defined methods, while an interface can only have constants and method signatures. A class can implement multiple interfaces, but can only inherit from one superclass.

Q: Can an abstract class be instantiated in Java? 

A: No, an abstract class cannot be instantiated in Java because it is an incomplete class that contains abstract methods.

Q: What is the purpose of the "final" keyword in Java?

 A: The "final" keyword in Java is used to indicate that a class, method, or variable cannot be changed or overridden. A final class cannot be subclassed, a final method cannot be overridden, and a final variable cannot be reassigned once it has been initialized.

Q: What is an interface in Java?

 A: An interface in Java is a collection of abstract methods and constants that can be implemented by any class. It provides a way to define a set of behaviours without providing any implementation.

Q: How is an interface different from a class in Java?

 A: An interface in Java is different from a class in that it cannot be instantiated, while a class can be. An interface only provides method signatures and constants, while a class can provide both method implementations and instance variables. A class can implement multiple interfaces, but can only inherit from one superclass.

Q: How do you implement an interface in Java?

 A: To implement an interface in Java, a class must use the "implements" keyword followed by the name of the interface. The class must then provide an implementation for all the methods in the interface.

Q: What is the purpose of default methods in Java interfaces? 

A: Default methods in Java interfaces were introduced in Java 8 to provide a way to add new methods to an interface without breaking existing code. Default methods provide a default implementation for a method in an interface, which can be overridden by a class that implements the interface.

Q: Can an interface extend another interface in Java?

 A: Yes, an interface can extend another interface in Java using the "extends" keyword. When one interface extends another interface, it inherits all the methods and constants from the parent interface. A class that implements the child interface must implement all the methods from both the child and parent interfaces.


Comments

Popular posts from this blog

FrontEnd - FAQs - Part 1

CoreJava - ClassesObjectsMethods - Assignment

Java Script - FAQs