There are a lot of differences between instance variable, class variable, and local variable in Java, and knowing them will help you to write correct and bug-free Java programs. Java is a full-featured programming language and provides different kinds of variables like static variables also called Class variable since it belongs to whole Class, non-static also called instance variable and local variables which vary in scope and value. Thank god Java doesn't have any register variable or auto scope like C Programming language, otherwise it would have so much detail to remember. static variables are a common source of error in may multi-threaded java program and does require a bit of carefulness while using it. On the other hand instance, the variable and the local variable have less sharing visibility than a static variable.
Monday, July 7, 2025
What is the Use of Interface in Java and Object Oriented Programming? [Answer]
Many times, I have seen questions like why should we use an interface in Java? If we can not define any concrete methods inside the interface the what is the user of the interface? Or even more common, What is the real use of the interface in Java? I can understand beginners asking this question when they just see the name of the method inside the interface and nothing else. It takes time to realize real goodness or actual use of interface or abstraction in Java or any object-oriented programming. One reason for this is a lack of experience in really modeling something real in the program using object-oriented analysis and design. In this article, I will try to answer this question and give you a couple of reasons to use the interface in your code. If you have a good understanding of Object-oriented basics like Polymorphism, then you know that it allows you to write flexible code.
Labels:
interface
,
java
,
object oriented programming
,
programming
5 Benefits of using interface in Java and Object Oriented Programming
Interface in Java is a simple concept but many programmers fail to realize
their actual use, including me. When I started learning Java programming, I
learned interface is something where you can declare functions but cannot
define them. To me, they were useless at that time because there was no code
to do anything. It took me years to realize how useful an interface can
be. They are not intended to do things but they are actually facilitators.
They provide abstraction and give the flexibility to change your program in the future. That's why it's very important for a Java programmer to understand the benefits of interfaces and realize how and when to use them in code.
Labels:
interface
,
java
,
object oriented programming
,
programming
Difference between Abstraction and Encapsulation in Java? OOP Question Answer
Both Abstraction and Encapsulation are two of the four basic OOP concepts which allow you to model real-world things into objects so that you can implement them in your program and code. Many beginners get confused between Abstraction and Encapsulation because they both look very similar. If you ask someone what is Abstraction, he will tell that it's an OOP concept which focuses on relevant information by hiding unnecessary detail, and when you ask about Encapsulation, many will tell that it's another OOP concept which hides data from the outside world. The definitions are not wrong as both Abstraction and Encapsulation do hide something, but the key difference is on intent.
Difference between Association, Composition and Aggregation in Java, UML and Object Oriented Programming
In Object-oriented programming, one object is related to another to use functionality and service provided by that object. This relationship between two objects is known as the association in object-oriented general software design and is depicted by an arrow in Unified Modelling Language or UML. Both Composition and Aggregation are the forms of association between two objects, but there is a subtle difference between composition and aggregation, which is also reflected by their UML notation. We refer to the association between two objects as Composition when one class owns other classes and other classes can not meaningfully exist, when the owner is destroyed.
Labels:
core java
,
design
,
object oriented programming
,
programming
Why Default or No Argument Constructor is Important in Java Class? Answer
Almost all Java developers know that compiler adds a default constructor or better known as a no-argument constructor in every Java class, but many of them forget that it only does when you don't provide any other constructor. This means it becomes the developers' responsibility to add a no-argument constructor if he is adding an explicit constructor. Now, Why it's important to provide a default constructor in Java, What happens if your class doesn't have a no-argument constructor? Well, this is how it's asked in many Java interviews, most commonly as part of Spring and Hibernate interviews.
Labels:
core java
,
object oriented programming
What is Type Casting in Java? Casting one Class to other class or interface Example
Type casting in Java is to cast one type, a class or interface, into another type i.e. another class or interface. Since Java is an Object-oriented programming language and supports both Inheritance and Polymorphism, It’s easy that Super class reference variable is pointing to SubClass objects but the catch here is that there is no way for Java compiler to know that a Superclass variable is pointing to SubClass object. This means you can not call a method that is declared in the subclass. In order to do that, you first need to cast the Object back into its original type. This is called type casting in Java. You can type cast both primitive and reference type in Java. The concept of casting will be clearer when you will see an example of type casting in the next section.
Labels:
coding
,
core java
,
object oriented programming
What is Constructor in Java with Example – Constructor Chaining and Overloading
What
is constructor in Java
Constructor in Java is a block of code which is executed at the time of
Object creation. But other than getting called, Constructor is entirely
different than methods and has some specific properties like name of the constructor
must be same as name of Class. Constructor also can not have any return type,
constructor’s are automatically chained by using this
keyword and super. Since Constructor is used to create object, object
initialization code is normally hosted in Constructor. Similar to the method you
can also overload the constructor in Java.
Labels:
core java
,
object oriented programming
What is Object in Java and Object Oriented Programming? Example Tutorial
Object in Java
Object in Java programming language or any other Object-oriented
programming language like C++ is the core of the OOPS concept and that's why the name.
Class
and Object along with Inheritance,
Polymorphism,
Abstraction
and Encapsulation
form the basis of any Object-oriented programming language e.g. Java. Objects are
instances of Class, Class defines blueprints and Objects are things that are
created based upon that blueprint. Object is also known as instances in Java,
e.g. When we say an instance of String class, we actually mean an Object of
String class. The object has state and behavior in Java.
Labels:
coding
,
core java
,
object oriented programming
,
programming
Open Closed Design Principle in Java - Benefits and Example
Great Example of Open Closed Design Principle
I am a big fan of design pattern articles and love to read articles on design patterns and recently wrote about decorator design pattern in Java, Observer pattern, static factory pattern and Singleton pattern. Today I come across this good article on open closed design patterns, what I like most is there example and clear way of explanation, first example is true value and it will help you understand open closed principle very quickly and second example is also not bad.
Labels:
core java
,
object oriented programming
How to use Class in Java Programming - Example
When I first about Class in Java I just thought what is this Class in Java and from that date to now Whenever we talk about java its really incomplete without classes, every one who are little bit familiar with java knows it’s purely object oriented language means every thing we discuss in java as object .so its very important for learner or anyone who is keen to know about java should know about java class then only they can move forward on java world.
In this article we will see what Java Class, Example of Class in Java is and what makes a Java Class including members, field and method.
Labels:
core java
,
object oriented programming
What is Inheritance in Java and OOP Tutorial - Example
Inheritance in Java is an Object oriented or OOPS
concepts, which allows to emulate real world Inheritance behavior, Inheritance
allows code reuse in Object oriented programming language e.g. Java. Along with
Abstraction,
Polymorphism
and Encapsulation,
Inheritance forms basis of Object-oriented programming. Inheritance is
implemented using extends keyword in Java and When one
Class extends another Class it inherit all non
private members including fields and methods. Inheritance in Java can be
best understand in terms of Parent and Child class,
also known as Super class and Sub class in Java programming language.
Labels:
core java
,
object oriented programming
,
programming
What is polymorphism in Java? Method overloading or overriding?
What is Polymorphism in Java
Polymorphism is an important Object oriented concept and is widely used in Java and other programming languages. Polymorphism in java is supported along with other concepts like Abstraction, Encapsulation, and Inheritance. Few words on the historical side; Polymorphism word comes from ancient Greek where poly means much so polymorphic are something which can take many forms. In this Java Polymorphism tutorial, we will see what is Polymorphism in Java, How Polymorphism has been implemented in Java e.g method overloading and overriding, why should we use Polymorphism and how can we take advantage of it polymorphism while writing code in Java. Along the way, we will also see a real-world example of using Polymorphism in Java.
Labels:
core java
,
object oriented programming
Can we declare a class Static in Java? Top Level and Nested static class Example
The answer to this question is both Yes and No, depending on whether you are talking about a top-level class or a nested class in Java. You cannot make a top-level class static in Java, the compiler will not allow it, but you can make a nested class static in Java. A top-level class is a class that is not inside another class. It may or may not be public like you can have more than one class in a Java source file and only needs to be public, whose name must be the same as the name of the file, rest of the class or interface on that file may or may not be public. On the other hand, a nested class is a class inside a top-level class. It is also known as the inner class or member class in Java.
Law of Demeter in Java - Principle of least Knowledge - Real life Example
The Law of Demeter also known as the principle of least knowledge is a coding principle, which says that a module should not know about the inner details of the objects it manipulates. If a code depends upon the internal details of a particular object, there is a good chance that it will break as soon as the internal of that object changes. Since Encapsulation is all about hiding internal details of an object and exposing only operations, it also asserts the Law of Demeter. One mistake many Java programmer makes it exposing internal detail of object using getter methods and this is where the principle of least knowledge alerts you.
Labels:
coding
,
core java
,
design patterns
,
object oriented programming
Subscribe to:
Posts
(
Atom
)