Showing posts with label OCPJP. Show all posts
Showing posts with label OCPJP. Show all posts
Serialization in Java

Serialization in Java

Serialization is the process of converting Java objects into a stream of bytes. The stream of bytes can be transmitted through a network connection, stored in a database as a BLOB object or saved as a binary file. The stored or transmitted stream of bytes can be reconstructed to Java object later. This article explains Serialization in Java, using a simple step by step example.
Note: The sample codes provided in this tutorial, use try-with-resources blocks to auto close the input and output streams. You need Java 7 or latest version to compile and run these codes. For more information about try-with-resources, visit to this tutorial: Try With Resources
Create a Student class with the attributes name, and age.
public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}
Create a Main class as shown below, to create an object of Student.
public class Main {

    public static void main(String[] args) {
        Student stu = createStudent();
        System.out.println("Name: " + stu.getName());
        System.out.println("Age: " + stu.getAge());
    }

    /**
     * Create a sample Student object.
     *
     * @return a Student object.
     */
    public static Student createStudent() {
        // Create a Student object
        Student stu = new Student();
        stu.setName("Alice");
        stu.setAge(24);

        return stu;
    }
}

Read More

Immutability Of String

Java defines some objects as immutable objects; developers cannot modify those objects once they are created. String objects and wrapper class objects are the best examples for immutable objects. This article covers the immutable objects and the reason for having immutable objects using String as an example.

Before getting into the actual content, it is required to have a basic knowledge about the equality operator (==). Usually equality operator is used to compare two primitive data types, but it has a different meaning when it is used with references. To compare the content of two objects, equals method is used and the equality operator is used to check whether two references are referring same object.
public class EqualityOperator {
    public static void main(String[] args) {
        Student stu1 = new Student(100);
        Student stu2 = new Student(100);
        Student stu3 = stu1;
        if (stu1 == stu2) { // false
            System.out.println("stu1 == stu2");
        }
        if (stu1.equals(stu2)) { // true
            System.out.println("stu1 equals stu2");
        }
        if (stu1 == stu3) { // true
            System.out.println("stu1 == stu3");
        }
    }
}
For the complete project click on this link.

In the above example stu1 == stu2 returns false, because they refer two different objects. stu1 == stu3 returns true because both references are referring the same object.

Read More

OOP: Polymorphism

This article explains the Object Oriented Concept, Polymorphism. If you are new to Object Oriented Programming, visit this link and get an introduction to OOP. Polymorphism is the ability of an entity to behave in different forms. Take a real-world example; the Army Ants. There are different size of ants in the same ant colony with different responsibilities; workers are in different sizes and the queen is the largest one. This is a polymorphic behavior where the entities have a unique feature while they share all other common attributes and behaviors.



Read More
Introduction to Interface (with Java 8 Features)

Introduction to Interface (with Java 8 Features)

Interfaces are used in Java to provide a template to developers and to avoid dead diamond problem in multiple inheritance. In an interface all the fields (variables) are by default public, static and final. For an example in the following code both the MIN_SIZE and MAX_SIZE are public, static and final constants.
interface Size {
    int MIN_SIZE = 1;
    public static final int MAX_SIZE = 10;
}
For the complete project click on this link.

Up to Java 1.7 version, all the methods declared in interfaces are public and abstract by default. Since Java 1.8, an interface can have default methods and static methods as well. Therefore, the updated rule is:
An interface can have default methods and static methods. Any other methods are implicitly public and abstract. All the fields declared in an interface are implicitly public, static and final constants.
interface Super {
    /**
     * An abstract method. By default it is public and abstract.
     */
    void print();

    /**
     * Default method, introduced in Java 8.
     */
    public default void doStuff() {
        System.out.println("Hello world");
    }

    /**
     * Static method in interface, introduced in Java 8.
     */
    public static void sayHello() {
        System.out.println("Hello");
    }
}
For the complete project click on this link.

Read More
OOP: Abstraction

OOP: Abstraction

Abstraction is one of the four Object Oriented Concepts. This article briefly explains the concept and the purpose of abstraction. In object oriented programming all the problems are mapped as an interaction of objects. However, there are some real world objects which are just abstract ideas. For example take Vehicle as an object. In real world do we have an object Vehicle? Let me to briefly explain my question. If I say car, in your mind you may have an image of car. If I say motorbike, you may have a mind picture of a motorbike. If I say Vehicle, what kind of image you have in your mind? Nothing, because Vehicle is just a grouping name used in real world. Rather than saying I have a car and a motorbike, I can say I have two vehicles. That is the only purpose of having a term Vehicle.

Read More
OOP: Inheritance

OOP: Inheritance

This article is the third article in the series of Object Oriented Programming tutorials, explaining the OOP concept - Inheritance. The same problem used in the Encapsulation tutorial is used here to explain Inheritance. The ABC bookshop needs software to maintain its billing procedures and stock management. This bookshop sells books, magazines, newsletters, and DVDs. In the last tutorial, we have developed a Book class.
public class Book {
    private String title;
    private String author;
    private double price;

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setPrice(double price) {
        if (price > 0) {
            this.price = price;
        } else {
            System.out.println("Invalid price");
        }
    }

    public double getPrice() {
        return this.price;
    }

    public void sell(int qty) {
        System.out.println("Total: $" + (qty * price));
    }
}
This time we are going to create a Magazine class with the following attributes and behaviors. Do not forget to use encapsulation when creating the class.
Read More
OOP: Encapsulation

OOP: Encapsulation

Object-Oriented Programming has four major concepts, which are Encapsulation, Inheritance, Abstraction, and Polymorphism. This article deals with the first concept Encapsulation. Consider this problem. The ABC bookshop needs software to maintain its billing procedures and stock management. This bookshop sells books, magazines, newsletters, and DVDs. The first step you need to do is developing a Book class to represent the books with the following attributes and behaviors. Remember that, attributes are represented by instance variables and the behaviors are represented by instance methods.
Read More
Object Oriented Programming

Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm used to develop modern applications. OOP allows developers to map real-world problems, exactly in the same manner in your desired object-oriented programming language, therefore developers can plan their design without any high-level technical interpretation. This tutorial provides an introduction to OOP and upcoming series of tutorials provide a brief explanation of the OOP concepts.

Read More

Contact Form

Name

Email *

Message *