Basic Java Concepts

In this chapter, we will talk about core Java concepts. In order to understand the more advanced Java topics, it is essential to understand the core Java concepts in full. In the later chapters, we will discuss these concepts in more detail.

What is a Java class?

A Java class is a template that defines the state and behavior for the objects that can be initialized from it. The properties of a Java class define the state for an object of its type while behaviors of an object are defined by the methods defined in its class.

What is an abstract class?

An abstract class is a class type that is declared with the abstract keyword. Abstract classes cannot be initialized and they are meant to be extended by other classes. Abstract classes are not required to contain abstract methods.

What is a Java object?

A Java object is an instance of a Java class whose state and behavior is defined in its class. All instances of a Java class (in other words, all the objects initialized from a Java class) share the same behavior and properties. A Java object has access to all the properties and behavior defined in its class.

What is a Java variable?

A Java variable is a piece of memory in the JVM that has a name and holds a value. The variable can be a primitive like int or long that comes predefined with the Java language, it can be a predefined Java class like String, or some other class that we define.

What are Java fields?

The variables that define the properties of a Java class are called fields, properties or instance variables of that class. It is legal for a Java class to have no instance variables. Objects initialized from a class share the same properties with same or different values set for them. The properties of an object, along with their values are referred to as the state of the object.

What is a Java keyword?

Keywords in Java are reserved terms that cannot be used as variable names. The reason for this is that each one of them has a predefined meaning in the Java language and using them for purposes other than what they were built for would cause a compiler error.

What is a Java method?

A Java method is a set of Java instructions put together to execute a task. A Java method can belong to only one class but different Java classes can have methods with same name or structure. Methods of a class define the behavior of objects initialized from that class.

What is a method signature?

A method’s name and parameters together are called a method’s signature. Other than these two, no other component of a method declaration is part of the method signature. No two methods in a class can have the same signature, even if they have different method types.

What is an abstract method?

An abstract method cannot have a method body and is declared with the abstract keyword. Only abstract classes and interfaces can have abstract methods. Abstract methods are meant to be overridden by subclasses or implementations.

What are Java data types?

The type of a Java variable determines what kind of data the variable can point to.The type of a variable can be a Java primitive type or an object type. Primitive data types, like int, short, char that are not initialized and just hold a simple value. On the other hand, an object type is initialized from a Java class.

A primitive Java variable of type int with value 3:

int x = 3;

An object variable of type String with value “good”:

String s = “good”;

What are Java identifiers?

An identifier is the name for a Java component defined in a class that is used while referring to that component. Properties of a class, methods of a class and everything else that is defined inside the class is a component of that class. Java language has rules defined for naming class components and not following these rules causes a compilation error.

What is a package?

A Java package is a directory structure that groups together Java classes that serve similar purposes. For example, in an application, classes that process transactions might go to one package while classes that update user information might go into another one. Choosing which class goes into which package is up to the developer.

What is an interface?

A Java interface is a reference type that acts as a contract for a Java class that implements it. An interface is used only for defining abstract methods and cannot be initialized. The implementation of the methods defined in an interface are up to the implementing class.

What is inheritance?

In Java, a class can inherit the fields and methods of another class by extending that class. This concept is known as inheritance and it is one of the core components of object-oriented programming. The class that is extended is called the super class, parent class or base class of the class that extends it. The class that extends the parent class is called a child class or a sub class.

What is class access?

A class has access to another class if it can:

  • Initialize that class
  • Extend that class
  • Access methods and instance variables defined in that class

Class access has different levels and these are determined by access modifiers.

What are access modifiers?

Access modifiers are Java keywords that are used to determine which other classes can access a class’ properties or methods. There are 4 access modifiers in Java: public, private, protected and default.

  • public: Any method and class can access such properties or methods from outside.
  • private: Only the methods inside the same class can access such properties or methods.
  • protected: Only methods of classes inside the same package and methods of the subclasses can access such variables or methods.
  • default access: When there’s no access modifier preceding the field or the method, its access level is default. This is the same as protected except such instance variables and methods cannot be accessed from subclasses.

Now, let’s see these in action in a simple Java class.

public class Person {

String firstName;

String lastName;

int age;

public void sayHi() {

System.out.println(“Hi”);

}

}

Here we define a class named Person. Person class has three properties: firstName, lastName and age. These are the fields or instance variables of the Person class. firstName and lastName are String type variables while age is an int (integer) type variable. Person class has one method, sayHi(). All these can be translated as, every object initialized from the Person class has a firstName, lastName and age but these can have different values for every instance. In addition, every Person object can sayHi().

In Java, an object is initialized from a class using the new keyword. A new Person object is initialized from a Person class as follows:

Person person = new Person();

1 4 2 3

Here, person is the name we gave to the object, therefore the identifier. In this statement, in part 1 we declare the variable person to be an object variable of type Person. Following that, new keyword allocates the memory in JVM for a new Person object in part 2. Part 3 initializes a new Person object. Assigning a value to a Java variable is done in part 4. Using the assignment operator, =, we assign the new Person object to the person variable.

Now let’s add a main method to the Person class and see these concepts in action.

public class Person {

1 String firstName;

2 String lastName;

3 int age;

4

5 public void sayHi() {

6 System.out.println(“Hi”);

7 }

8

9 public static void main(String [] args) {

10 Person person1 = new Person();

11 Person person2 = new Person();

12

13 person1.firstName = “James”;

14 person1.lastName = “West”;

15 person2.age = 48;

16

17 person2.firstName = “Miranda”;

18 person2.lastName = “Wellington”;

19 person2.age = 39;

20

21 System.out.println(person1.firstName + “ “ + person1.lastName + “ is “ + person1.age + “ years old.”);

22 System.out.println(person2.firstName + “ “ + person2.lastName + “ is “ + person2.age + “ years old.”);

23

24 person1.sayHi();

25 person2.sayHi();

}

}

The output is

James West is 48 years old.

Miranda Wellington is 39 years old.

Hi

Hi

Here, inside the main method, we define two Person instances, person1 and person2. person1 and person2 are object variables of type Person. Then we assign values to the fields of both classes. person1’s firstName field’s value is set to “James”, lastName field is set to “West” and age is set to 48. person2’s firstName field’s value is set to “Miranda”, lastName field is set to “Wellington” and age is set to 39. Now, each object’s instance variables have a unique set of values and therefore each object has its own state.

Line 21 constructs a sentence using firstObject’s fields’ values and prints

James West is 48 years old.

on the console.

Similarly, line 22 constructs a sentence using secondObject’s fields’ values and prints

Miranda Wellington is 39 years old.

on the console.

Following that, sayHi() method of both objects are called and they both print “Hi” on the console.

On lines 21 and 22, while printing on the console, we use a technique called string concatenation. Two strings can be concatenated using + operator. + operator can also be used to concatenate another variable, like an int, to a string. On line 21, person1.firstName is the string “James”, person1.lastName is the String “West” and person1.age is the int 48. These values are used in the string concatenation operation. So, the statement inside System.out.println is translated into “James” + “ “ + “West” + “ is “ + 48 + “ years “ + “ old”, which is concatenated into “James West is 48 years old” and then printed on the console. The same concept is used also on line 22.

We will take a closer look at how classes work in Chapter 5.

%d bloggers like this: