INHERITANCE, PACKAGES AND INTERFACES
1. What are hierarchical abstractions in Java?
Hierarchical abstractions refer to the organization of classes in a parent-child hierarchy, where child
classes inherit properties and behaviors from parent classes.
Example:
class Animal {
void makeSound() {
[Link]("Some sound");
}
}
class Dog extends Animal {
void makeSound() {
[Link]("Bark");
}
}
class Cat extends Animal {
void makeSound() {
[Link]("Meow");
}
}
2. Base Class Object
2.: What is a base class object in Java?
A base class object is an instance of the parent class from which subclasses inherit.
Example:
class Animal {
void eat() {
[Link]("Eating");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
[Link](); // Eating
}
}
3. Subclass and Subtype
3. What is the difference between a subclass and a subtype?**
A subclass is a class that inherits from another class, whereas a subtype refers to a type derived from
another type, ensuring substitutability.
Example:
class Animal {
void eat() {
[Link]("Eating");
}
}
class Dog extends Animal {
void bark() {
[Link]("Barking");
}
}
4. Forms of Inheritance: Specialization
[Link] is specialization in inheritance?
Specialization is when a subclass extends a parent class by adding new features.
Example:
class Vehicle {
void drive() {
[Link]("Driving");
}
}
class Car extends Vehicle {
void playMusic() {
[Link]("Playing music");
}
}
```
5. Using `super`
5: How is the `super` keyword used in inheritance?
The `super` keyword is used to refer to the immediate parent class object.
Example:
class Animal {
void eat() {
[Link]("Animal eating");
}
}
class Dog extends Animal {
void eat() {
[Link](); // Calls the eat method of Animal class
[Link]("Dog eating");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
}
}
6. `final` with Inheritance
6. What is the effect of using the `final` keyword in inheritance?
The `final` keyword prevents a class from being subclassed or a method from being overridden.
Example:
final class Animal {
void eat() {
[Link]("Eating");
}
}
// This will cause an error because Animal is final
// class Dog extends Animal {
// }
class Dog {
final void bark() {
[Link]("Barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Barking
}
}
```
7. Polymorphism and Method Overriding
[Link] is polymorphism in Java?
Polymorphism allows one interface to be used for a general class of actions. Method overriding is a form
of polymorphism.
Example:
class Animal {
void makeSound() {
[Link]("Some sound");
}
}
class Dog extends Animal {
void makeSound() {
[Link]("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
[Link](); // Bark
}
}
8. Abstract Classes
8. What is an abstract class in Java?
An abstract class cannot be instantiated and can contain abstract methods that must be implemented
by subclasses.
Example:
abstract class Animal {
abstract void makeSound();
void sleep() {
[Link]("Sleeping");
}
}
class Dog extends Animal {
void makeSound() {
[Link]("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
[Link](); // Bark
[Link](); // Sleeping
}
}
9. Packages
[Link] is a package in Java?
A package is a namespace that organizes classes and interfaces by functionality.
Example:
public class Main {
public static void main(String[] args) {
[Link]("Hello from a package!");
}
}
// Compile: javac -d . com/example/[Link]
// Run: java [Link]
10. Understanding CLASSPATH
10. What is CLASSPATH in Java?
CLASSPATH is an environment variable that tells the Java compiler where to look for classes and
packages.
# Set the CLASSPATH variable
export CLASSPATH=.:/path/to/your/classes
11. Importing Packages
11. How do you import a package in Java?
You use the `import` keyword to include a package in your class.
import [Link];
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("Hello");
[Link]([Link](0)); // Hello
}
}
12. Differences between Classes and Interfaces
[Link] is the difference between classes and interfaces in Java?
Classes can have implementation details, whereas interfaces only declare methods without
implementing them (unless they are default or static methods).
class Animal {
void eat() {
[Link]("Eating");
}
}
interface AnimalInterface {
void eat();
}
class Dog implements AnimalInterface {
public void eat() {
[Link]("Dog eating");
}
}
13. Defining an Interface
13. How do you define an interface in Java?
An interface is defined using the `interface` keyword.
Example:
interface Animal {
void eat();
14. Implementing an Interface
14. How do you implement an interface in Java?
A class implements an interface using the `implements` keyword.
Example:
interface Animal {
void eat();
class Dog implements Animal {
public void eat() {
[Link]("Dog eating");
}
15. Applying Interfaces
15. How do you apply interfaces in Java?
You can create instances of classes that implement interfaces and use the interface type to refer to
them.
Example:
interface Animal {
void eat();
class Dog implements Animal {
public void eat() {
[Link]("Dog eating");
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
[Link](); // Dog eating
16. Variables in Interface
[Link] interfaces have variables in Java?
Yes, but all variables in interfaces are implicitly `public`, `static`, and `final`.
Example:
interface Animal {
int legs = 4; // implicitly public, static, and final
void eat();
class Dog implements Animal {
public void eat() {
[Link]("Dog eating with " + legs + " legs");
17. Extending Interfaces
17. Can interfaces extend other interfaces in Java?
Yes, interfaces can extend multiple interfaces.
Example:
interface Animal {
void eat();
interface Mammal extends Animal {
void walk();
class Dog implements Mammal {
public void eat() {
[Link]("Dog eating");
}
public void walk() {
[Link]("Dog walking");
18. Exploring `[Link]`
18. How do you use `[Link]` package for input and output operations?
The `[Link]` package provides classes for system input and output through data streams, serialization,
and the file system.
Example:
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
} catch (IOException e) {
[Link]();
}
}
}