Wednesday, 19 August 2015

Java Tutorial : Inheritance IS-A Relationship Animal (Implements)

🚀 Master Java with Ram N Java!

Want to become a pro developer? Join our community for simple, daily coding lessons!

SUBSCRIBE NOW

Understanding "IS-A" Relationship with Interfaces

In Java, the IS-A relationship isn't just about extending classes. It also applies to Interfaces. When a class implements an interface, it establishes an IS-A relationship by committing to a specific behavior or "contract."

What is an "IS-A" Relationship in this context?

When a class like Dog implements an interface like Animal, we can say that a Dog IS-A Animal. This allows us to use the interface type to refer to any class that implements it, which is the heart of polymorphism.

Example: If Dog implements Pet, then Dog IS-A Pet.

Why Use Interfaces for IS-A?

  • Multiple Inheritance: Java doesn't allow extending multiple classes, but a class can implement multiple interfaces!
  • Design Flexibility: You can define what an object should do without dictating exactly how it should do it.
  • Standardization: It ensures that all child classes follow a common set of rules.

Key Takeaways for Beginners

  1. The implements keyword is used to create an IS-A relationship with an interface.
  2. It promotes loose coupling and makes your code more adaptable to change.
  3. It's a fundamental concept for understanding advanced Java frameworks.

Watch More Java Tutorials


Click here to watch in Youtube :
https://www.youtube.com/watch?v=oXtnBDbGo5k&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Inheritance IS-A Relationship Animal (Implements) 
Java Tutorial : Inheritance IS-A Relationship Animal (Implements) 
Java Tutorial : Inheritance IS-A Relationship Animal (Implements) 

AnimalDemo.java
/**
 * Animal is the Super Interface of Mammal,Parrot and Dove classes
 */
interface Animal
{

}

/**
 * Mammal is the subclass of Animal Interface.
 */
class Mammal implements Animal
{

}

/**
 * Dog is the subclass of both Mammal class and Animal Interface.
 */
class Dog extends Mammal
{

}

/**
 * Elephant is the subclass of both Mammal class and Animal Interface.
 */
class Elephant extends Mammal
{

}

/**
 * Tiger is the subclass of both Mammal class and Animal Interface.
 */
class Tiger extends Mammal
{

}

/**
 * Parrot is the subclass of Animal Interface.
 */
class Parrot implements Animal
{

}

/**
 * Dove is the subclass of Animal Interface.
 */
class Dove implements Animal
{

}

public class AnimalDemo
{
    public static void main(String args[])
    {

        Mammal mammal = new Mammal();
        Dog dog = new Dog();
        Elephant elephant = new Elephant();
        Tiger tiger = new Tiger();

        System.out.print("Mammal IS-A Animal : ");
        System.out.println(mammal instanceof Animal);
        System.out.println();

        System.out.print("Dog IS-A Mammal : ");
        System.out.println(dog instanceof Mammal);
        System.out.print("Dog IS-A Animal : ");
        System.out.println(dog instanceof Animal);
        System.out.println();

        System.out.print("Elephant IS-A Mammal : ");
        System.out.println(elephant instanceof Mammal);
        System.out.print("Elephant IS-A Animal : ");
        System.out.println(elephant instanceof Animal);
        System.out.println();

        System.out.print("Tiger IS-A Mammal : ");
        System.out.println(tiger instanceof Mammal);
        System.out.print("Tiger IS-A Animal : ");
        System.out.println(tiger instanceof Animal);
        System.out.println();

        Parrot parrot = new Parrot();
        Dove dove = new Dove();

        System.out.print("Parrot IS-A Animal : ");
        System.out.println(parrot instanceof Animal);
        System.out.println();

        System.out.print("Dove IS-A Animal : ");
        System.out.println(dove instanceof Animal);

    }
}
Output
Mammal IS-A Animal : true
Dog IS-A Mammal : true
Dog IS-A Animal : true

Elephant IS-A Mammal : true
Elephant IS-A Animal : true

Tiger IS-A Mammal : true
Tiger IS-A Animal : true

Parrot IS-A Animal : true

Dove IS-A Animal : true

To Download DisplayAllHeadersApp Project Click the below link
https://sites.google.com/site/javaee4321/java/InheritanceDemo-IS-A-Animal-ImplementsApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Tutorials