Inheritance in Java:
Inheritance is an essential mechanism in Java that allows developers to define a new class based on an existing class. In other words, inheritance is a process in which one class acquires the properties of another class. The new class is called the subclass or derived class, and the existing class becomes the superclass or base class. Subclass extends the superclass by inheriting all its properties and methods.
Syntax for using inheritance in Java:
To use inheritance in Java, you need to define a new class that extends the existing class. Here's the syntax:
class Subclass extends Superclass {
// fields and methods
}
In this syntax, the keyword extends is used to indicate that Subclass is derived from Superclass. Now, all the properties and methods of Superclass are inherited by Subclass.
The "Super" keyword in Java:
The super keyword in Java is used to call the constructor, properties, and methods of the superclass from the subclass.
Syntax for using the 'super' keyword in Java:
Here's the syntax for calling the super keyword in Java:
- super() -- it calls the constructor of the superclass
- super.property -- it calls the property of the superclass
- super.method() -- it calls the method of the superclass
Example:
class Person {
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Student extends Person {
private String course;
Student(String name, int age, String course) {
super(name, age); // calls the constructor of the superclass
this.course = course;
}
public void display() {
System.out.println("Name: " + super.name); // calls the property of the superclass
System.out.println("Age: " + super.age);
System.out.println("Course: " + course);
}
}
The "this" keyword in Java:
The this keyword in Java is a reference variable that refers to the current object where the method or constructor is being called.
Syntax for using the 'this' keyword in Java:
Here's the syntax for calling the 'this' keyword in Java:
- this.property -- it refers to the property of the current object
- this.method() -- it refers to the method of the current object
Example:
class Person {
private String name;
private int age;
Person(String name, int age) {
this.name = name; // refers to the name property of the current object
this.age = age; // refers to the age property of the current object
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Polymorphism in Java:
Polymorphism is a concept in Java that allows objects of different classes to be used interchangeably based on their common attributes and behaviors. Polymorphism is an essential feature of object-oriented programming.
Types of polymorphism in Java:
- Compile-time polymorphism
- Runtime polymorphism
Compile-time Polymorphism:
Compile-time polymorphism allows methods to have different signatures, but they have the same name. The compiler determines which method to call based on the number and type of arguments passed to the method.
Example:
class Math {
public int add(int x, int y) {
return x + y;
}
public int add(int x, int y, int z) {
return x + y + z;
}
}
Runtime Polymorphism:
Runtime polymorphism allows objects of a subclass to be used in place of a superclass based on the common properties and behaviors. The method to be called is determined at runtime based on the actual object type.
Example:
class Animal {
public void sound() {
System.out.println("Animal is making a sound");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Meow");
}
}
class Dog extends Animal {
public void sound() {
System.out.println("Woof");
}
}
class Main {
public static void main(String[] args) {
Animal animal1 = new Cat();
Animal animal2 = new Dog();
animal1.sound(); // Meow
animal2.sound(); // Woof
}
}
Conclusion:
Inheritance, super keyword, this keyword, and polymorphism are all concepts that are used in Java programming. Using inheritance in Java can help developers to reuse code and create new classes based on the existing class. The super keyword is used to call the methods and properties of the superclass from the subclass. The this keyword is used to refer to the current object where the method or constructor is being called. Polymorphism is a powerful concept in Java that allows objects of different classes to be used interchangeably based on their common attributes and behaviors.