Friday, August 19, 2016

Java Object Oriented Programming Concept

Java OOPS concepts

Object Oriented Programming is a paradigm that provides many concepts such as inheritance, data binding, polymorphism etc.
Simula is considered as the first object-oriented programming language. The programming paradigm where everything is represented as an object, is known as truly object-oriented programming language.
Smalltalk is considered as the first truly object-oriented programming language.


OOPs (Object Oriented Programming System)

Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation


Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

Class
Collection of objects is called class. It is a logical entity.

Inheritance
When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc. In java, we use method overloading and method overriding to achieve polymorphism. Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Static Polymorphism (compile time polymorphism/ Method overloading):
The ability to execute different method implementations by altering the argument used with the method name is known as method overloading. For Example We have three print methods each with different arguments. When you properly overload a method, you can call it providing different argument lists, and the appropriate version of the method executes.

Dynamic Polymorphism (run time polymorphism/ Method Overriding)
When you create a subclass by extending an existing class, the new subclass contains data and methods that were defined in the original superclass. In other words, any child class object has all the attributes of its parent. Sometimes, however, the superclass data fields and methods are not entirely appropriate for the subclass objects; in these cases, you want to override the parent class members.

Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction.

Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines. A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here. Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes. Programmers sometimes refer to encapsulation as using a “black box,” or a device that you can use without regard to the internal mechanisms. A programmer can access and use the methods and data contained in the black box but cannot change them.

Advantage of OOPs over Procedure-oriented programming language
  1. OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows.
  2. OOPs provides data hiding whereas in Procedure-oriented prgramming language a global data can be accessed from anywhere.
  3. OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.

Wednesday, August 17, 2016

Java Comments

Java Comments

Comments

The purpose of including comments in your code is to explain what the code is doing. Java supports both single and multi-line comments. All characters that appear within a comment are ignored by the java compiler.



Single-line comment

A single-line comment starts with two forward slashes and continues until it reaches the end of the line.
For example:

// this is a single-line comment
X= 5; //Assign 5 to Variable X

Adding comments as you write code is a good practice,because they provide clarification and understanding when you need to refer back to it, as well as for others who might need to read it.


Multi-line comments

Java also supports comments that span multiple lines.
You start this type of comment with a forward slash followed by an asterisk, and end it with an asterisk followed by a forward slash.
For example:


/* 
* This is also a Comment 
* Spanning multiple lines
*/

Note that Java does not support nested mutli-line comments.
However, you can nest Single-line comments within multi-line comments.


/*
* JavaSunderesan - MultiLine Comments 
// a Single Line Comment
*/

Documentation Comments

Documentation comments are special comments that have the appearance of multi-line comments, with the difference being that they generate external documentation of your source code. These begin with a forward slash followed by two asterisks, and end with an asterisk followed by an forward slash.
For Example:


/** Welcome To Java Sunderesan
* Copyright (C)2016 King  . All Rights Reserved
*/

Javadoc is a tool which comes with JDK and it is used for generating Java Code documentation in HTML format from Java source code which has required documentation in a predefined format.
When a documentation comment begins with more than two asterisks, Javadoc assumes that you want to create a "box" around the comment in the source code. It simply ignores the extra asterisks.
For Example:


/**********
* Welcome To Java Sunderesan Copyright (C)2016 King . All Rights Reserved
*/

Monday, August 8, 2016

Write a program for a String must contain three digit using String.format()

Write a program for a String must contain three digit using String.format()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
      package com.javasunderesan;

public class StringThreeDigit {

 public static void main(String[] args) {
  try
  {
   int no = 0;
   String formattingString = null;
   for (int i = 0; i < 10; i++) {
    formattingString = String.format("Dev -  %03d", no);
    System.out.println(formattingString);
    no++;
   }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}
Output
Dev -  000
Dev -  001
Dev -  002
Dev -  003
Dev -  004
Dev -  005
Dev -  006
Dev -  007
Dev -  008
Dev -  009

Write a Program to Add spaces before and After a String using String.format()

Write a Program to Add spaces before and After a String using String.format()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
      package com.javasunderesan;

public class AddSpaceBeforeAndAfterString {
 public static void main(String[] args) {
  try {
   String msg = "java";
   System.out.println(String.format("%8s", msg)); // adding spaces
               // before
               // string ;
   System.out.println(String.format("%-8s", msg));// adding spaces
               // after
               // string
   // 8 is a number of characters String and spaces

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Output::

    java
java    

Java - String Format

String Format

Description:

String.format() method helps us to format the given string.
It replaces each format item in a specified string with the text equivalent of a corresponding object's value.


Syntax:
Here is the syntax of this method:

public static String format(String format, Object... args)


Parameters:
format : format of the string.
args : arguments for the format string. It may be zero or more.

Return Value
formatted string

String.format() Example Program
Write a Program to Add spaces before and After a String using String.format()
Write a program for a String must contain three digit using String.format()