How to declare a package?

The package statement is used to declare packages. The word package is a keyword in Java. This statement is not technically needed to write a complete Java program. However, if it appears, it must be the first executable statement in the program. Only comments or white spaces are allowed before a package statement. There can be only one package declaration in a program. If no package declaration appears, the class will belong to the default package.

For example, the following source code file defines a class named ClassA in the package named package4:

package package4;

  • Share/Bookmark

The this keyword

The this keyword refers to the currently executing instance of a class. It is commonly used to access members from within constructors, instance methods, and instance accessors. However, the static member functions do not have a this pointer. For example:

using System;
public class Students
{
public string name;
public string subcode;
//Constructor.
public Students(string name, string subcode)
{
//Use of this keyword to qualify members hidden by similar names.
this.name = name;
this.subcode = subcode;
}
}

The this keyword is used to pass an object as a parameter to other methods. For example:

Cal_Marks(this);

  • Share/Bookmark

What is an instanceOf operator?

The instanceof operator is a binary operator that determines at runtime whether its left operand is an instance of its right operand.
Syntax:

obj instanceof type

where, obj (the left operand) must be an instance of a class and type (the right operand) may be a class, interface, or an array type. The instanceof operator evaluates to true if the left operand is of the specified type or can be cast to the specified type. Otherwise, it evaluates to false. The instanceof operator cannot be used with primitive data types.

  • Share/Bookmark
  1. If a class does not implement any or all of an interface method, then it should be declared as abstract.
  2. An abstract class is made up of one or more abstract methods that are declared but left unimplemented.
  3. Abstract classes cannot be instantiated, but they can be subclassed.
  4. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
  • Share/Bookmark