Series: OOPS in Depth

Java OOPS in Depth

Access Specifiers Permissions and Restrictions

Access specifiers, as the name indicates, specify the access to our code for other classes – whether other classes can access or not and if permitted, to what extent they can access. Java includes access modifiers also which are quiet different from access specifiers. Java comes with four access specifiers. They are public protected default …

Access Specifiers Permissions and Restrictions Read More »

Access Modifiers Java Meaning Functionality

We know access specifiers specify the access and access modifiers modify the access of variables, methods and classes. In general, an access modifier works between the classes of the same application or within the classes of same package. Note: I have seen some people calling access modifiers also as specifiers. It seems for them Java …

Access Modifiers Java Meaning Functionality Read More »

Java private abstract Method with Example

An abstract class is permitted to have both concrete and abstract methods. In abstract class, no method (either concrete or abstract) can be private. The reasons are very simple and when known looks as of common sense. Let us make a list to remember. Abstract methods in an abstract class cannot be private but can …

Java private abstract Method with Example Read More »

Abstract class constructor and Interface abstract

These are two different concepts which can be explained easily by a novice having a little bit knowledge of abstract classes and interfaces. It is advised to read abstract classes before going through this topics. It gives thorough understanding of abstract classes where some concepts you may not aware of, perhaps. Before going further let …

Abstract class constructor and Interface abstract Read More »

Method overloading vs Method Overriding Java

Method overloading and method overriding are two concepts supported by a OOPs language. Method overloading Definition: Using the same method number of times in the same class but with different parameters is known as method overloading. Advantages: The same method call gives different outputs when called different times. For example, getArea() method may give sometimes …

Method overloading vs Method Overriding Java Read More »

abstract class vs final class

Abstract class and Final class are very different basically in their nature and usage in Java language. Both are very contrary in their functionality. Following table gives the differences: abstract class vs final class. Property Abstract class Final class Subclassing Should be subclassed to override the functionality of abstract methods Can never be subclassed as …

abstract class vs final class Read More »

Can you write a final constructor in Java?

When a class is inherited, the subclass inherits the methods and variables of super class but not constructors. For this reason, constructors are not members of class; only variables and methods are members of a class. As members are inherited, they are permitted by subclass to override with its own functionality. When constructors are not …

Can you write a final constructor in Java? Read More »

Can you make Static Constructor in Java?

Java does not permit to declare a constructor as static. Following are the reasons. 1. Static means for the same class. For example, static methods cannot be inherited. 2. With static, "this" reference (keyword) cannot be used. "this" is always linked to an object. A constructor always belongs to some object. 3. If a constructor …

Can you make Static Constructor in Java? Read More »

Does Java support static local variables inside method?

To answer this question, first we must know the concept of static with variables. Static variables are clearly and more elaborately discussed in . For our question sake, I quote a few points about static variables. A static variable does not maintain encapsulation. There will be only one location for static variable accessed by all …

Does Java support static local variables inside method? Read More »

Can you make Private Constructor in Java?

Declaring a private constructor is a very rare usage. First of all can we do it? Yes, a constructor can be private and a private constructor can be overloaded also. Following program illustrates. public class Test { private Test() { System.out.println(“I am default private constructor”); } private Test(int x) { System.out.println(“I am overloaded private constructor. …

Can you make Private Constructor in Java? Read More »

Can we override static method in Java with Example?

The super class static methods cannot be overridden by sub class because they do not represent the state of the object or with static methods no encapsulation exists. For this reason, the super class static methods are not part of subclass. When they are not part of subclass, they cannot be overridden. Let us write …

Can we override static method in Java with Example? Read More »

Can you create interface reference variables?

interface reference variables It is stated in my earlier posting Can you create instance of interface? that we cannot create objects of interfaces but possible as an inner class. Now the present question is can we create interface reference variables and if permitted what is the use of these reference variables as interface does not …

Can you create interface reference variables? Read More »

Java Overload Main Method

Java Overload Main Method Summary: It is a frequent asked interview question. Your answer is YES and is explained in simple terms in this tutorial "Java Overload Main Method". Method overloading is nothing but using the same method in the same class a number of times with different parameters. Just like any other method, the …

Java Overload Main Method Read More »

Rules of Access Specifiers in Method Overriding

Access Specifiers Method Overriding: The only rule says: "The subclass overridden method cannot have weaker access than super class method". Let us see the above rule practically in Access Specifiers Method Overriding. class Test { protected void display() // protected specifier { System.out.println(“Hello 1”); } } public class Demo extends Test { void display() // …

Rules of Access Specifiers in Method Overriding Read More »

static vs non-static Java

Java novices are not much aware of static vs non-static or static to non-static calling. Let us find the rules through a program. In the following code, show() method is non-static and display() is static. The question is "Can we call a non-static method from static method?". Everyone says simply "no" because it requires an …

static vs non-static Java Read More »

Abstract class main() method

Can you have Abstract class main() method? Yes, definitely, because main() is a concrete method and abstract class allows concrete methods. But what you can you do with the main() when you are not allowed to create objects of abstract classes. But, you can create objects of another class and use other class methods by …

Abstract class main() method Read More »

Public methods and Private Variables

What does it mean by Public methods and Private Variables? Declaring methods as public and variables as private is a programming technique and comes with its own advantages. We know private variables or methods cannot be accessed by composition (has-a relationship) by another class where as public members can be accessed. A private variable can …

Public methods and Private Variables Read More »

What is blank final variable with Example?

1. What is blank final variable or uninitialized final variable? A final variable declared but not assigned is known as a blank final variable or uninitialized final variable. Introduced with JDK 1.1. 2. How to initialize or give a value for an uninitialized final variable? It is possible with a constructor call only. That is, …

What is blank final variable with Example? Read More »