How do I check if a class represent a primitive type?

Java uses class objects to represent all eight primitive types. A class object that represents a primitive type can be identified using the isPrimitive() method call. void is not a type in Java, but the isPrimitive() method returns true for void.class.

package org.kodejava.lang.reflect;

public class IsPrimitiveDemo {
    public static void main(String[] args) {
        IsPrimitiveDemo.get(int.class);
        IsPrimitiveDemo.get(String.class);
        IsPrimitiveDemo.get(double.class);
        IsPrimitiveDemo.get(void.class);
    }

    private static void get(Class<?> clazz) {
        if (clazz.isPrimitive()) {
            System.out.println(clazz.getName() +
                    " is a primitive type.");
        } else {
            System.out.println(clazz.getName() +
                    " is not a primitive type.");
        }
    }
}

Here is the result of the program:

int is a primitive type.
java.lang.String is not a primitive type.
double is a primitive type.
void is a primitive type.

How do I get information regarding class name?

package org.kodejava.lang.reflect;

import java.util.Date;

public class ClassNameDemo {
    public static void main(String[] args) {
        Date date = new Date();

        // Gets the Class of the date instance.
        Class<?> clazz = date.getClass();

        // Gets the name of the class.
        String name = clazz.getName();
        System.out.println("Class name     : " + name);

        // Gets the canonical name of the class.
        String canonical = clazz.getCanonicalName();
        System.out.println("Canonical name : " + canonical);

        // Gets the simple name of the class.
        String simple = clazz.getSimpleName();
        System.out.println("Simple name    : " + simple);
    }
}

Here are the information printed out by the program:

Class name     : java.util.Date
Canonical name : java.util.Date
Simple name    : Date

How do I create and implement abstract class?

Abstract class is a class that have one or more methods declared, but not defined. Abstract class cannot have instances. This class uses in inheritance to take advantage of polymorphism. To declare that a class is abstract, use the abstract keyword in front of the class keyword in the class definition.

Methods in abstract class that have no definition are called abstract methods. The declaration for an abstract method ends with a semicolon and you specify the method with the abstract keyword to identify it as such. The implementation is left to the sub classes.

package org.kodejava.example.fundamental;

public abstract class Animal {
    private String species;

    public Animal(String species) {
        this.species = species;
    }

    public abstract void makeASound();

    public String getSpecies() {
        return species;
    }

    public static void main(String[] args) {
        Animal pig = new Pig("Warthog");
        pig.makeASound();
    }
}

The Pig class extends the Animal class. Because the Animal class contains an abstract method makeASound() the Pig class must implements this method or else the Pig will also become an abstract class.

package org.kodejava.example.fundamental;

public class Pig extends Animal {

    public Pig(String species) {
        super(species);
    }

    @Override
    public void makeASound() {
        System.out.println("oink oink");
    }
}

How do I extend classes in Java?

Inheritance is one of object-oriented programming concepts. This concept allows classes to inherit commonly used state and behavior from other classes. Inheritance is the way to put commonly used states and behaviors into one class and reuse it.

The class that inherits all the attributes from other class is called as sub class. While, the class that inherited is called as superclass. You can use the extends keyword in class definition to inherit from other classes.

When you apply the final keyword to the class declaration you will make the class final, a final class cannot be extended by other class.

For example below we have, a Truck class and a Sedan that derived from a Car class.

package org.kodejava.basic;

public class CarDemo {
    public static void main(String[] args) {
        Car car = new Car();
        car.setBrand("Honda");
        System.out.println("Brand = " + car.getBrand());

        // The setBrand() and getBrand() is inherited from the Car
        // class.
        Truck truck = new Truck();
        truck.setBrand("Ford");
        System.out.println("Brand = " + truck.getBrand());
        int loadCapacity = truck.getLoadCapacity();

        // The setBrand(), getBrand() and setNumberOfSeat methods
        // are is inherited from the Car class.
        Sedan sedan = new Sedan();
        sedan.setBrand("Hyundai");
        System.out.println("Brand = " + sedan.getBrand());
        sedan.setNumberOfSeat(2);
        int gearType = sedan.getGearType();
    }
}

Here the definition of the Car, the Truck and the Sedan classes.

package org.kodejava.basic;

public class Car {
    private String type;
    private String brand;
    private String model;
    private int numberOfSeat;

    public Car() {
    }

    public Car(String type, String brand, String model) {
        this.type = type;
        this.brand = brand;
        this.model = model;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getNumberOfSeat() {
        return numberOfSeat;
    }

    public void setNumberOfSeat(int numberOfSeat) {
        this.numberOfSeat = numberOfSeat;
    }

    public String getCarInfo() {
        return "Type: " + type
                + "; Brand: " + brand
                + "; Model: " + model;
    }
}
package org.kodejava.basic;

public class Truck extends Car {
private int loadCapacity;

    public Truck() {
    }

    public Truck(String type, String brand, String model) {
        super(type, brand, model);
    }

    public int getLoadCapacity() {
        return loadCapacity;
    }

    public void setLoadCapacity(int loadCapacity) {
        this.loadCapacity = loadCapacity;
    }

    @Override
    public String getCarInfo() {
        return "Type: " + getType()
                + "; Brand: " + getBrand()
                + "; Model: " + getModel()
                + "; Load capacity: " + getLoadCapacity();
    }
}
package org.kodejava.basic;

public class Sedan extends Car {
    private int gearType;

    public Sedan() {
    }

    public int getGearType() {
        return gearType;
    }

    public void setGearType(int gearType) {
        this.gearType = gearType;
    }
}

How do I create a class in Java?

A class is a specification or blueprint from which individual objects are created. A class contains fields that represent the object’s states and methods that defines the operations that are possible on the objects of the class.

The file name that contains the definition of a class is always the same as the public class name and the extension is .java to identify that the file contains a Java source code.

A class has constructors, a special method that is used to create an instance or object of the class. When no constructor define a default constructor will be used. The constructor method have the same name with the class name without a return value. The constructors can have parameters that will be used to initialize object’s states.

Here is a Person.java file that defines the Person class.

package org.kodejava.example.fundamental;

public class Person {
    private String name;
    private String title;
    private String address;

    /**
     * Constructor to create Person object
     */
    public Person() {

    }

    /**
     * Constructor with parameter
     *
     * @param name
     */
    public Person(String name) {
        this.name = name;
    }

    /**
     * Method to get the name of person
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * Method to set the name of person
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Method to get the title of person
     *
     * @return title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Method to set the title of person
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Method to get address of person
     *
     * @return address
     */
    public String getAddress() {
        return address;
    }

    /**
     * Method to set the address of person
     *
     * @param address
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * Method to get name with title of person
     *
     * @return nameTitle
     */
    public String getNameWithTitle() {
        String nameTitle;
        if (title != null) {
            nameTitle = name + ", " + title;
        } else {
            nameTitle = name;
        }
        return nameTitle;
    }

    /**
     * Method used to print the information of person
     */
    @Override
    public String toString() {
        return "Info [" +
                "name='" + name + ''' +
                ", title='" + title + ''' +
                ", address='" + address + ''' +
                ']';
    }
}

Here is a ClassExample.java file that defines the ClassExample class that use the Person class.

package org.kodejava.example.fundamental;

public class ClassExample {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("Andy");
        person.setTitle("MBA");
        person.setAddress("NY City");
        System.out.println(person);

        String nameTitle1 = person.getNameWithTitle();
        System.out.println("Name with title: " + nameTitle1);

        Person person2 = new Person("Sarah");
        String nameTitle2 = person2.getNameWithTitle();
        System.out.println("Name with title 2: " + nameTitle2);
    }
}