
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

1K+ Views
Yes, we can create a class without a name in Java using the anonymous class. It is a type of inner class which does not have a name and whose instance is created at the time of the creation of the class itself. You can create this class in a single statement using the new keyword. Creating multiple instances of this class is not allowed. You can use anonymous classes in case where you need to override methods of a class or an interface for a one-time use, and you don't want to create a separate named class for it. ... Read More

4K+ Views
A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have the same name as their class and, have no return type.There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.Example Live Demopublic class Example { public Example(){ System.out.println("This is the constructor of the class example"); } public static void main(String args[]) { Example obj = new Example(); } }OutputThis is the constructor of the class exampleFinal methodWhenever you ... Read More

758 Views
In Java final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don’t, a compile-time error is generated.Example Live Demopublic class Student { public final String name; public final int age; public void display(){ System.out.println("Name of the Student: "+this.name); ... Read More

3K+ Views
In Java, final is the access modifier which can be used with a filed, class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Initializing a final variableOnce you declare a final variable, it is a must to initialize it. You can initialize the final instance variable −At the time of declaration as.public final String name = "Raju"; public final int age = 20;Within an instance (non-static) block.{ this.name = "Raju"; this.age = 20; }Within a default constructor.public final ... Read More

5K+ Views
In Java, final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them.Still, if you try to declare final variables without initialization that will generate a ... Read More

2K+ Views
In Java final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is finale it cannot be extended.Extending a final classWhen we try to extend a final class that will lead to a compilation error saying “cannot inherit from final SuperClass”ExampleIn the following Java program, we have a final class with name SuperClass and we are trying to inherent it from another class (SubClass).final class SuperClass{ public void display() { ... Read More

6K+ Views
The “this" keyword in Java is used as a reference to the current object within an instance method or a constructor. Using this, you can refer to the members of a class, such as constructors, variables, and methods. Calling a Method using "this" From a Constructor Yes, as mentioned, we can call all the members of a class (methods, variables, and constructors) from instance methods or constructors. Example In the following Java program, the Student class contains two private variables, name and age, with setter methods and a parameterized constructor that accepts these two values. From the constructor, we are ... Read More

1K+ Views
A static method or, block belongs to the class and these will be loaded into the memory along with the class. You can invoke static methods without creating an object. (using the class name as reference).Whereas "this" in Java acts as a reference to the current object. But static contexts(methods and blocks) doesn't have any instance they belong to the class.In a simple sense, to use “this” the method should be invoked by an object, which is not always necessary with static methods.Therefore, you cannot use this keyword from a static method.ExampleIn the following Java program, the class ThisExample contains a private ... Read More

6K+ Views
The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables, and methods.Returning “this”Yes, you can return this in Java i.e. The following statement is valid.return this;When you return "this" from a method the current object will be returned.ExampleIn the following Java example, the class Student has two private variables name and age. From a method setValues() we are reading values from user and assigning them to these (instance) variables and returning the current object.public class Student ... Read More

1K+ Views
The super keyword in Java is a reference to the object of the parent/superclass. Using it, you can refer/call a field, a method or, a constructor of the immediate superclass.Referring to a field using super keywordAs mentioned above, you can refer to an instance filed/variable of the immediate superclass from an instance method or, a constructor of a subclass, using the "super" keyword.If a class extends another class, a copy of members of the parent class will be created in the subclass i.e. Using the object of the subclass you can access the members of both subclass and the superclass.If ... Read More