You are on page 1of 4

In Java, the "this" keyword is a reference to the current instance of the class.

It is commonly used
to differentiate between instance variables and method parameters with the same name, and to
access instance members within an instance method.
class Student
{
// Instance variables
private String name;
private int age;

// Constructor
public Student(String name, int age)
{
// Use 'this' keyword to refer to instance variables
this.name = name;
this.age = age;
}

// Instance method to display student data


public void display()
{
// Use 'this' keyword to refer to instance variables within instance methods
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
}

public class thisJava


{
public static void main(String[] args)
{
// Create a Student object and call the display method
Student student1 = new Student("Aman", 20);
student1.display();
// Create another Student object
Student student2 = new Student("Ajay", 22);
student2.display();
}
}
Method overloading
Method overloading in Java allows us to define multiple methods in the same class with the
same name but different parameters. The compiler decides which method to call based on the
number and types of arguments passed during the method invocation. Method overloading
provides a way to create more readable and reusable code by providing multiple ways to call a
method based on the input arguments.
To overload a method, you need to define multiple methods with the same name but different
parameter lists. The parameter lists must differ either in the number of parameters or their types.
The return type of the method doesn't play a role in method overloading.
Rules for Method Overloading:
Method Name: All overloaded methods must have the same name.
Parameter List: The overloaded methods must have a different number or types of parameters.
This can include different data types, different order of parameters, or different variable
argument lists (varargs).
Return Type: The return type of the method does not play a role in method overloading. It can be
the same or different for overloaded methods.
Access Modifiers and Exceptions: The access modifiers (public, private, protected, or default)
and checked exceptions thrown by the method can be the same or different for overloaded
methods.
public class methodOverloading
{
public int add(int a, int b)
{
return a + b;
}
public int add(int a, int b, int c)
{
return a + b + c;
}
public double add(double a, double b)
{
return a + b;
}
// Method to concatenate two strings
public String add(String str1, String str2)
{
return str1 + str2;
}

public static void main(String[] args)


{
methodOverloading mo= new methodOverloading();
System.out.println("Sum of 5 and 10: " + mo.add(5, 10));
System.out.println("Sum of 1, 2, and 3: " + mo.add(1, 2, 3));
System.out.println("Sum of 2.5 and 3.7: " + mo.add(2.5, 3.7));
System.out.println("Concatenated String: " + mo.add("Hello ", "World"));
}
}
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class
(the child or subclass) to inherit properties and behaviors from another class (the parent or
superclass). In Java, inheritance is implemented using the "extends" keyword. By using
inheritance, you can create a hierarchy of classes, which promotes code reusability and helps
organize your code more effectively.
Superclass (Parent Class):
The superclass is the class from which other classes will inherit properties and methods. It is
defined using the class keyword.
Subclass (Child Class):
A subclass is a class that inherits from the superclass. It is defined using the class keyword and
the extends keyword, followed by the name of the superclass.
Single Inheritance:
Java supports single inheritance, which means a class can inherit from only one superclass. In
other words, a subclass can have only one direct parent class. This is the most common type of
inheritance used in Java and other programming languages that support OOP.
Multiple Inheritance (Interface Inheritance):
Java does not support multiple inheritance through classes, where a class directly inherits from
multiple classes. However, it supports multiple inheritance through interfaces. An interface can
declare a set of abstract methods that classes can implement, and a class can implement multiple
interfaces.
Notes: Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in
java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If
A and B classes have the same method and you call it from child class object, there will be
ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if we
inherit 2 classes. So whether you have same method or different, there will be compile time
error.
Multilevel Inheritance:
In multilevel inheritance, a class is derived from another class, which, in turn, is derived from
another class. This creates a chain of inheritance. Java supports multilevel inheritance.
Hierarchical Inheritance:
Hierarchical inheritance occurs when multiple classes inherit from a single superclass. In this
case, the superclass is at the top of the hierarchy, and several subclasses inherit from it.
Hybrid Inheritance (Rarely Used):
Hybrid inheritance is a combination of multiple and hierarchical inheritance. While Java does not
support multiple inheritance through classes, it is possible to create a hybrid inheritance structure
using interfaces and classes.

You might also like