IS-A Relationship (Inheritance)
The "IS-A" relationship, also known as inheritance, is a fundamental concept in object-oriented programming (OOP) where a class is derived from another class, inheriting its properties and behaviors. In Java, the "IS-A" relationship is achieved using the "extends" keyword. It signifies that a subclass is a specialized type of its superclass.
In Java, the "IS-A" relationship is established through inheritance, where a subclass inherits the properties and behaviors of its superclass. This relationship signifies that the subclass is a specialized type of its superclass, forming an "IS-A" hierarchy.
Here are two examples that illustrate the "IS-A" relationship in Java:
Example 1: Vehicle Hierarchy
public class Vehicle {
protected String brand;
public Vehicle(String brand) {
this.brand = brand;
}
public void start() {
System.out.println("Starting the " + brand + " vehicle.");
}
}
public class Car extends Vehicle {
public Car(String brand) {
super(brand);
}
public void drive() {
System.out.println("Driving the " + brand + " car.");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota");
car.start();
car.drive();
}
}
Starting the Toyota vehicle. Driving the Toyota car.
Example 2: Animal Hierarchy
public class Animal {
protected String species;
public Animal(String species) {
this.species = species;
}
public void makeSound() {
System.out.println("The " + species + " is making a sound.");
}
}
public class Dog extends Animal {
public Dog() {
super("Dog");
}
public void fetch() {
System.out.println("The dog is fetching.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
dog.fetch();
}
}
The Dog is making a sound. The dog is fetching.
In the first example, we have a vehicle hierarchy where the "Vehicle" class is the superclass and the "Car" class is a subclass. The "Car" class inherits the "brand" property and the "start()" method from the "Vehicle" class and adds its own method "drive()". The "Main" class demonstrates the usage of the "Car" class, showcasing the "IS-A" relationship where a car is a specialized type of a vehicle.
In the second example, we have an animal hierarchy where the "Animal" class is the superclass and the "Dog" class is a subclass. The "Dog" class inherits the "species" property and the "makeSound()" method from the "Animal" class and adds its own method "fetch()". The "Main" class demonstrates the usage of the "Dog" class, highlighting the "IS-A" relationship where a dog is a specialized type of an animal.
Functional and Technical Questions on IS-A Relationship (Inheritance) in Java
Functional Questions
Q1: What is the purpose of the "IS-A" relationship in Java?
A: The "IS-A" relationship establishes a hierarchical relationship between classes, allowing for code reuse and creating specialized types of existing classes.
Q2: How is the "IS-A" relationship implemented in Java?
A: In Java, the "IS-A" relationship is implemented using the "extends" keyword to create a subclass that inherits properties and behaviors from a superclass.
Q3: What are the benefits of using the "IS-A" relationship?
A: The benefits of the "IS-A" relationship include code reuse, polymorphism, and the ability to create specialized types that inherit and extend the functionality of existing classes.
Some more Functional Questions
Q4: What is the difference between an abstract class and an interface in the context of the "IS-A" relationship?
A: An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods. Additionally, a class can extend only one abstract class but can implement multiple interfaces.
Q5: Can a subclass inherit from multiple superclasses in Java?
A: No, Java does not support multiple inheritance. A subclass can inherit from only one superclass but can implement multiple interfaces.
Q6: How does method overriding work in the "IS-A" relationship?
A: Method overriding occurs when a subclass provides its own implementation of a method that is already defined in the superclass. The subclass can override methods to customize behavior while still maintaining the "IS-A" relationship.
Coding Questions on IS-A Relationship (Inheritance) in Java
Q1: Create a class hierarchy representing different types of vehicles. Each vehicle should have a "start()" method that prints a message. Create subclasses for different types of vehicles and demonstrate the "IS-A" relationship by invoking the "start()" method of each subclass.
public class Vehicle {
public void start() {
System.out.println("Starting the vehicle.");
}
}
public class Car extends Vehicle {
public void start() {
System.out.println("Starting the car.");
}
}
public class Bike extends Vehicle {
public void start() {
System.out.println("Starting the bike.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.start();
Car car = new Car();
car.start();
Bike bike = new Bike();
bike.start();
}
}
Q2: Create a class hierarchy representing different types of animals. Each animal should have a "sound()" method that prints a message. Create subclasses for different types of animals and demonstrate the "IS-A" relationship by invoking the "sound()" method of each subclass.
public class Animal {
public void sound() {
System.out.println("The animal is making a sound.");
}
}
public class Dog extends Animal {
public void sound() {
System.out.println("The dog is barking.");
}
}
public class Cat extends Animal {
public void sound() {
System.out.println("The cat is meowing.");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound();
Dog dog = new Dog();
dog.sound();
Cat cat = new Cat();
cat.sound();
}
}
Q3:What will be the output of below code?
class Animal {
String name = "Animal";
public void printName() {
System.out.println(name);
}
}
class Dog extends Animal {
String name = "Dog";
public void printName() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.printName();
}
}
Solution:
The output of the above code will be "Dog". Even though the reference variable "animal" is of type "Animal", the actual object being referred to is an instance of the "Dog" class. Therefore, when the method "printName()" is called, it invokes the overridden method in the "Dog" class, which prints "Dog" instead of "Animal". This behavior is possible due to the "IS-A" relationship between the classes.
Go To OOPS Page Click Here