Java Inheritance

Java inheritance is a feature of object-oriented programming that enables a new class to be created based on an existing class. The new class, called a subclass, inherits the properties and methods of the existing class, called the superclass, and can add new properties and methods or override the inherited ones.

In Java, inheritance is implemented using the “extends” keyword. When a subclass extends a superclass, it gains access to all the public and protected members of the superclass, including fields, methods, and constructors. Private members of the superclass are not accessible by the subclass.

the extends keyword is used to create a subclass that inherits properties and behaviors from an existing class, called the superclass or parent class.

To create a subclass that extends a superclass, you use the extends keyword followed by the name of the superclass. A subclass inherits properties and behaviors from an existing class, called the superclass or parent class.

For example:

class Animal {
    // properties and behaviors of Animal class
}

class Dog extends Animal {
    // additional properties and behaviors of Dog class
}

In this example, the Dog class extends the Animal class, which means it inherits all the properties and behaviors of the Animal class, and can also define additional properties and behaviors of its own.

For example, consider the following superclass:

public class Shape {
    protected int x;
    protected int y;

    public Shape(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void draw() {
        System.out.println("Drawing shape at " + x + ", " + y);
    }
}

And the following subclass:

public class Circle extends Shape {
    private int radius;

    public Circle(int x, int y, int radius) {
        super(x, y);
        this.radius = radius;
    }

    public void draw() {
        System.out.println("Drawing circle at " + x + ", " + y + " with radius " + radius);
    }
}

In this example, the Circle class extends the Shape class, and it inherits the x and y fields and the draw() method from the Shape class. The Circle class also has its own radius field and a custom implementation of the draw() method.

Super Keyword

In Java, the super keyword is used to refer to the superclass or parent class of a subclass. It is used to access the properties and methods of the superclass, or to call the constructor of the superclass.

There are several ways to use the super keyword in Java:

  1. To call the constructor of the superclass: When you create an object of a subclass, the constructor of the superclass is automatically called. However, you can also call the constructor of the superclass explicitly using the super() method. For example:
    class Animal {
        public Animal(String name) {
            System.out.println("Animal's name is " + name);
        }
    }
    
    class Dog extends Animal {
        public Dog(String name) {
            super(name); // call the constructor of Animal class
            System.out.println("Dog's name is " + name);
        }
    }
    
    Dog myDog = new Dog("Rufus");

    In this example, the Dog class calls the constructor of the Animal class using the super() method, passing in the name parameter. This prints “Animal’s name is Rufus” followed by “Dog’s name is Rufus”.

  2. To access the superclass properties and methods: You can also use the super keyword to access the properties and methods of the superclass. For example:
    class Animal {
        protected int age;
    
        public Animal(int age) {
            this.age = age;
        }
    
        public void speak() {
            System.out.println("Animal is speaking");
        }
    }
    
    class Dog extends Animal {
        public Dog(int age) {
            super(age);
        }
    
        public void speak() {
            super.speak(); // call the speak() method of Animal class
            System.out.println("Dog is barking");
        }
    
        public void printAge() {
            System.out.println("Animal's age is " + super.age); // access the age property of Animal class
        }
    }
    
    Dog myDog = new Dog(3);
    myDog.speak(); // prints "Animal is speaking" followed by "Dog is barking"
    myDog.printAge(); // prints "Animal's age is 3"

    In this example, the Dog class calls the speak() method of the Animal class using the super.speak() method. It also accesses the age property of the Animal class using the super.age syntax.

The super keyword is an important aspect of Java inheritance, allowing developers to access and use the properties and methods of the superclass in a subclass.

IS-A Relationship

In Java, inheritance is a mechanism by which one class acquires the properties (methods and variables) of another class. The “is-a” relationship is the fundamental principle of inheritance in Java.

The “is-a” relationship indicates that one object is a specific type of another object. For example, if we have a class called “Animal,” and we create a subclass called “Dog,” we can say that “Dog is an Animal.” This relationship is expressed in Java code by using the keyword “extends” to create a subclass:

class Animal {
   // properties and methods
}

class Dog extends Animal {
   // additional properties and methods specific to dogs
}

In this example, “Dog” is the subclass and “Animal” is the superclass. “Dog” inherits all the properties and methods of “Animal,” and it can also have its own properties and methods that are specific to dogs.

The “is-a” relationship is important in Java because it allows us to create classes that are more specific and specialized than their superclass. It also allows us to reuse code from existing classes, which can save time and reduce errors in programming.

The instanceof Keyword

In Java, the instanceof keyword is used to test if an object is an instance of a specific class or an instance of a subclass of that class. The instanceof operator returns a boolean value that indicates whether the object is an instance of the specified class or not.

The syntax for using the instanceof keyword is as follows:

object instanceof class

Here, object is the object that you want to test, and class is the name of the class that you want to test against.

For example, consider the following code snippet:

class Animal {
   // properties and methods
}

class Dog extends Animal {
   // additional properties and methods specific to dogs
}

Animal myAnimal = new Animal();
Dog myDog = new Dog();

if (myAnimal instanceof Animal) {
   System.out.println("myAnimal is an instance of Animal");
}

if (myDog instanceof Animal) {
   System.out.println("myDog is an instance of Animal");
}

if (myDog instanceof Dog) {
   System.out.println("myDog is an instance of Dog");
}

In this example, we create an instance of Animal called myAnimal, and an instance of Dog called myDog. We then use the instanceof keyword to test whether each object is an instance of Animal or Dog.

The first if statement will print “myAnimal is an instance of Animal” because myAnimal is an instance of the Animal class. The second if statement will also print “myDog is an instance of Animal” because Dog is a subclass of Animal. Finally, the third if statement will print “myDog is an instance of Dog” because myDog is an instance of the Dog class.

In summary, the instanceof keyword is used to test whether an object is an instance of a particular class or a subclass of that class. It is a useful tool for performing runtime type checks and implementing polymorphism in Java programs.

HAS-A relationship

In Java, the “HAS-A” relationship is a principle of object-oriented programming that indicates that an object of one class has a reference to an object of another class. This relationship is also known as composition, aggregation or containment.

The “HAS-A” relationship is used when one class needs to use the functionality or properties of another class, but the relationship between the two classes is not hierarchical like in the “IS-A” relationship.

For example, let’s consider a Car class that has a Engine class, which is necessary for the Car to function:

public class Engine {
    // Properties and methods
}

public class Car {
    private Engine engine;

    public Car() {
        engine = new Engine();
    }

    // Methods and properties
}

In this example, the Car class “HAS-A” relationship with the Engine class, which means that a Car object contains a reference to an Engine object.

We can also have multiple classes that are related through the “HAS-A” relationship. For example, a Company class may have a list of Employee objects:

public class Employee {
    // Properties and methods
}

public class Company {
    private List<Employee> employees;

    public Company() {
        employees = new ArrayList<>();
    }

    // Methods and properties
}

In this example, the Company class “HAS-A” relationship with the Employee class, and the Company object contains a list of Employee objects.

In summary, the “HAS-A” relationship in Java indicates that one object of a class has a reference to an object of another class, and it is used when one class needs to use the functionality or properties of another class.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial