Day-31:this ,super,inner class in java
Java Dynamic Binding: Using the super Keyword When invoking a superclass version of an overridden method the super keyword is used so that we can utilize parent class method while using dynamic binding. Example: Using the super Keyword class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal b = new Dog(); // Animal reference but Dog object b.move(); // runs the method in Dog class } } output: Animals can move Dogs can walk and run ** Example of Java Dynamic Binding** In this example, we've created two classes Animal and Dog where Dog class extends Animal class. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect. package com.tutorialspoint; class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } } public class Tester { public static void main(String args[]) { Animal a = new Animal(); // Animal reference and object // Dynamic Binding Animal b = new Dog(); // Animal reference but Dog object a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class } } output: Animals can move Dogs can walk and run ** this Keyword in Java** The this keyword in Java is a reference variable that refers to the current object. It is used within an instance method or a constructor to access members of the current object such as instance variables, methods, and constructors. Usage The this keyword is primarily used in the following scenarios: To refer to the current class instance variable. To invoke the current class method. To invoke the current class constructor. To pass the current object as a parameter to a method. To return the current object from a method. To pass the current object as a parameter to a constructor. To access the outer class instance from an inner class. Syntax this.memberName; this.methodName(); this(parameters); Examples Example 1: Referencing Instance Variables[TBD] public class ThisExample { int a; int b; ThisExample(int a, int b) { this.a = a; this.b = b; } void display() { System.out.println("a: " + this.a + ", b: " + this.b); } public static void main(String[] args) { ThisExample obj = new ThisExample(10, 20); obj.display(); } } Example 2: Invoking a Method[TBD] public class ThisMethodExample { void display() { System.out.println("Hello, World!"); } void invokeDisplay() { this.display(); } public static void main(String[] args) { ThisMethodExample obj = new ThisMethodExample(); obj.invokeDisplay(); } } Example 3: Invoking a Constructor[TBD] public class ThisConstructorExample { int a; int b; ThisConstructorExample() { this(10, 20); } ThisConstructorExample(int a, int b) { this.a = a; this.b = b; } void display() { System.out.println("a: " + a + ", b: " + b); } public static void main(String[] args) { ThisConstructorExample obj = new ThisConstructorExample(); obj.display(); } } Java Inner Classes Java Inner Classes are classes defined within another class. They are used to logically group classes that are only used in one place, increase encapsulation, and can lead to more readable and maintainable code. Inner classes can access the members (including private members) of the outer class. Types of Inner Classes Non-static Nested Class (Inner Class): Associated with an instance of the outer class. Static Nested Class: Not associated with an instance of the outer class and can access only static members of the outer class. Local Inner Class: Defined within a block, typically a method. Anonymous Inner Class: A class without a name, used to instantiate objects with certain "on-the-fly" functionality. Examples Example 1: Non-static Inner Class[TBD] public class OuterClass { private String message = "Hello from Outer Class"; class InnerClass { void display() { System.out.println(message); } } public static void main(String[] args) { OuterClass outer = new OuterClass(); OuterClass.InnerClass inner = outer.new InnerClass(); inner.display(); } } Example 2: Static Nested Class[TBD] public class OuterClass { private static String message = "Hello from Static Nested Class"; static class StaticNestedClass { void display() { Syste

Java Dynamic Binding: Using the super Keyword
When invoking a superclass version of an overridden method the super keyword is used so that we can utilize parent class method while using dynamic binding.
Example: Using the super Keyword
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}
output:
Animals can move
Dogs can walk and run
** Example of Java Dynamic Binding**
In this example, we've created two classes Animal and Dog where Dog class extends Animal class. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.
package com.tutorialspoint;
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class Tester {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
// Dynamic Binding
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
output:
Animals can move
Dogs can walk and run
** this Keyword in Java**
The this keyword in Java is a reference variable that refers to the current object. It is used within an instance method or a constructor to access members of the current object such as instance variables, methods, and constructors.
Usage
The this keyword is primarily used in the following scenarios:
To refer to the current class instance variable.
To invoke the current class method.
To invoke the current class constructor.
To pass the current object as a parameter to a method.
To return the current object from a method.
To pass the current object as a parameter to a constructor.
To access the outer class instance from an inner class.
Syntax
this.memberName;
this.methodName();
this(parameters);
Examples
Example 1: Referencing Instance Variables[TBD]
public class ThisExample {
int a;
int b;
ThisExample(int a, int b) {
this.a = a;
this.b = b;
}
void display() {
System.out.println("a: " + this.a + ", b: " + this.b);
}
public static void main(String[] args) {
ThisExample obj = new ThisExample(10, 20);
obj.display();
}
}
Example 2: Invoking a Method[TBD]
public class ThisMethodExample {
void display() {
System.out.println("Hello, World!");
}
void invokeDisplay() {
this.display();
}
public static void main(String[] args) {
ThisMethodExample obj = new ThisMethodExample();
obj.invokeDisplay();
}
}
Example 3: Invoking a Constructor[TBD]
public class ThisConstructorExample {
int a;
int b;
ThisConstructorExample() {
this(10, 20);
}
ThisConstructorExample(int a, int b) {
this.a = a;
this.b = b;
}
void display() {
System.out.println("a: " + a + ", b: " + b);
}
public static void main(String[] args) {
ThisConstructorExample obj = new ThisConstructorExample();
obj.display();
}
}
Java Inner Classes
Java Inner Classes are classes defined within another class. They are used to logically group classes that are only used in one place, increase encapsulation, and can lead to more readable and maintainable code. Inner classes can access the members (including private members) of the outer class.
Types of Inner Classes
- Non-static Nested Class (Inner Class): Associated with an instance of the outer class.
- Static Nested Class: Not associated with an instance of the outer class and can access only static members of the outer class.
- Local Inner Class: Defined within a block, typically a method.
- Anonymous Inner Class: A class without a name, used to instantiate objects with certain "on-the-fly" functionality.
Examples
Example 1: Non-static Inner Class[TBD]
public class OuterClass {
private String message = "Hello from Outer Class";
class InnerClass {
void display() {
System.out.println(message);
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
inner.display();
}
}
Example 2: Static Nested Class[TBD]
public class OuterClass {
private static String message = "Hello from Static Nested Class";
static class StaticNestedClass {
void display() {
System.out.println(message);
}
}
public static void main(String[] args) {
OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass();
nested.display();
}
}
Example 3: Anonymous Inner Class[TBD]
public class OuterClass {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Running from Anonymous Inner Class");
}
};
new Thread(runnable).start();
}
}
Reference link:
https://www.tutorialspoint.com/java/java_dynamic_binding.htm
https://www.datacamp.com/doc/java/this
https://www.datacamp.com/doc/java/inner-classes