Exceptions in Java are events that occur when there is disruption to the normal flow of the program and the null pointer exception is one of these events. The NullPointerException occurs when a reference variable should point to an object but instead it points to null. Before we get into the details thought, it is important to understand reference types and how they work in Java.
Consider the following two lines:
Polymorphism refers to an objects ability to take on different forms. In Java, it is a simple, yet fundamental concept of objected oriented programming. In order to understand polymorphism, it is important to have a solid grasp of inheritance and interfaces in Java.
A class is considered to be polymorphic if it passes more than one “is-a” test. An “is-a” test is a simple way of identifying the type a class belongs to; if the class belongs to more than one type then it is considered to be polymorphic. Consider this example:
Access modifiers in Java are keywords that are used to specify the accessibility of fields, methods, constructors and classes. There are four access modifiers, each specifying a certain level of accessibility:
2. protected: can be accessed from within the class itself, the package and by subclasses
3. package-private (default): can be accessed within the class itself and the package only
4. private: can be accessed only within the class
Giving a field a public access modifier allows it to be accessed from anywhere. However, it is important to keep in mind that labelling fields…
Read this to ace every interview question regarding interfaces.
What are interfaces?
An interface is a reference type similar to a class but different in a sense that it can only contain abstract methods, default methods, static methods, (only default and static methods in interfaces can have a method body) nested types and constants. Interfaces cannot be instantiated; hence they do not have constructors. Instead, interfaces are implemented by classes (or extended by other interfaces).
What is a reference type?
Reference data types in Java are those which contain references to the address of dynamically created objects. …
Packages in Java correspond with the directory structure in your project. They are used to group and control accessibility of related classes, interfaces and sub-packages.
The advantages of using packages are:
In Java, a class can be derived from an existing class and when that happens, the derived class inherits the fields and the methods from that existing class. In Java, and many other languages, this is called inheritance. The class that is derived from an existing class is often referred to as the subclass or child class. The class that is being derived from is called the super class or the parent class.
Consider a simple inheritance digram below.
Static and final keywords in Java are at the core of the language and it is important to have an in-depth understanding of their function.
If you’ve every written a Java program of any scale, you’ve come across the “static” keyword. Your first program probably looked something like this:
In this short article, I am going to introduce and implement the binary tree data structure. I will also answer questions such as “What is a full binary tree and what is a complete binary tree?”and “How do I traverse a binary tree?”.
First, let’s cover the basic definition of a tree. In computer science, data structures such a arrays, stacks, queues and linked lists are all linear data structures; they differ from trees, which have a hierarchical structure. A tree data structure is composed of nodes; each tree has a root node which has zero or more child nodes…
Computer Science Student, Programming Enthusiast