Saturday, 19 July 2014

Basics of Java or Fundamental of Java or OOps Concepts

Basics of Java 

Java is a object oriented programming language. It supports the writing of many different kinds of executables: applications, applets, and servlets. To encourage programming on the computer, the mechanics of compiling and running a Java application are outlined. Object-oriented programming (OOP) is the dominant programming paradigm these days, having replaced the "structured," procedural programming techniques that were developed in the early 1970s. Java is totally object oriented, and it is impossible to program it in the procedural style that you may be most comfortable with.
                        In real world every thing is a object, so, java also developed in same concept. Object is created by class. Class is only blueprint or template. it contains members (field, method, constructor).
   
    class Person{ //To create the class use keyword class and give some name to class. here name is Person
             private String name;  // This is field which is type of String, and this will hold the name
             private String add;   
            Person(){    // This is constructor of Person class, 
            }                         
            public String getName(){ // method which with return type String.
                return name;
            }
          //...............other setter getter method
     }

We can create the Object for above Person class by several ways in java- 
  1. By using new operator like -Person person = new Person();
  2. By using Class.forName() - Person person= (Person) Class.forName("Person").newInstance();
  3. By using clone() if object is already present in heap then you can clone another similar object by using like Person anotherPerson =person.clone();
  4. By deserialization mechanism- if object is already serialized in some filesystem then deserilize is mechanism, which can give object. like-
    ObjectInputStream inStream = new ObjectInputStream(anInputStream );
    Person person= (Person) inStream.readObject();
OOPs Element
  1. Class
  2. Object
  3. Abstraction 
  4. Encapsulation
  5. Inheritance
  6. Polymorphism
Class is a blueprint as explain above topic. which responsible to create object and has member like field, constructor, method, static block, instance-Initializer block.
Field: field is nothing but the property/attribute of the class or object. which holds the data of specific type.
Method: method is nothing but the operation that an object can perform it define the behavior of object
Constructor: the purpose of Constructors and methods differ in three aspects of the signature: modifiers, return type, and name. Like methods, constructors can have any of the access modifiers: public, protected, private, or none (often called package or friendly). Unlike methods, constructors can take only access modifiers. Therefore, constructors cannot be abstract, final, native, static, or synchronized.and the name of constructor is same to class name. The purpose of constructor is to create an instance of a class.
static block is calls before the constructor and can be used to initialize the static field/attribute.
Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created. The initialization of the instance variable can be directly but there can be performed extra operations while initializing the instance variable in the instance initializer block. 
Sequence of code execution is- static, constructor, instance-initilizer block.
See below example and output to understand properly-
  1. class A{  
  2. A(){  
  3. System.out.println("parent class constructor invoked");  
  4. }  
  5. }  
  6. class B extends A{  
  7. B(){  
  8. super();  // after this line compiler put the instance-initilizer block, so after super instance-initilizer will //execute
  9. System.out.println("child class constructor invoked");  
  10. }  
  11.   
  12. {System.out.println("instance initializer block is invoked");}  //instance-initilizer block
  13.   
  14. public static void main(String args[]){  
  15. B b=new B();  
  16. }  

Output:parent class constructor invoked
       instance initializer block is invoked
       child class constructor invoked

Object

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Abstraction 

Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You cannot create an instance of the abstract class.
If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclass. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.
Let's create abstract class-
public abstract class Employee
{
   private String name;
   private String address;
   private int number;
   //setter and getter and constructor 
}
Here you can not create the object for Employee class - 
if you write below code -
Employee e = new Employee()// Compile error 

public class Salary extends Employee
{
   private double salary; //Annual salary 
}
 
Here, we cannot instantiate a new Employee, but 
if we instantiate a new Salary 
object, the Salary object will inherit the three fields 
and setter getter methods.

A class can even be declared as abstract even though it has no abstract methods.Abstract classes cannot be instantiated. That is, if a class is declared as abstract, no objects of that class can be created.

Encapsulation
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. For example car, you never worried about internal part of car like engine, how it works internally. you worry about outer part of car.

Inheritance
It is a fundamental concept of object-oriented programming.Let's take one example-
you work for a company at which managers are treated differently from other employees. Managers are, of course, just like employees in many respects. Both employees and managers are paid a salary. However, while employees are expected to complete their assigned tasks in return for receiving their salary, managers get bonuses if they actually achieve what they are supposed to do. This is the kind of situation that cries out for inheritance. Why? Well, you need to define a new class, Manager, and add functionality. But you can retain some of what you have already programmed in the Employee class, and all the fields of the original class can be preserved. More abstractly, there is an obvious "is–a" relationship between Manager and Employee. Every manager is an employee: this "is–a" relationship is the hallmark of inheritance.
Here is how you define a Manager class that inherits from the Employee class. You use the Java keyword extends to denote inheritance.
The keyword extends indicates that you are making a new class that derives from an existing class. The existing class is called the superclass, base class, or parent class. The new class is called the subclass, derived class, or child class
class Manager extends Employee
{
   private double bonus;
  //added methods and fields
    public void setBonus(double b) {
     bonus = b;
    }
}
In UML diagram we denote by (------------|> ) arrow  in this case Manager-------------|> Employee

Polymorphism
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.This is also decide at run time hence called runtime or dynamic binding.
For example, you can assign a subclass object to a super class variable.
Employee e;
e = new Employee(. . .); // Employee object expected
e = new Manager(. . .); // OK, Manager can be used as well
In the Java programming language, object variables are polymorphic. A variable of type Employee can refer to an object of type Employee or to an object of any subclass of the Employee class (such as Manager, Executive, Secretary, and so on).
 

1 comment:

  1. Java Class By Rk: Basics Of Java Or Fundamental Of Java Or Oops Concepts >>>>> Download Now

    >>>>> Download Full

    Java Class By Rk: Basics Of Java Or Fundamental Of Java Or Oops Concepts >>>>> Download LINK

    >>>>> Download Now

    Java Class By Rk: Basics Of Java Or Fundamental Of Java Or Oops Concepts >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete