Posts

Showing posts with the label java basics

Cloning in java with example

Cloning: Cloning is a technology to obtain exact copy of a plant, a bird an animal or a human being. This cloning technology available now. Cloning in java: Obtaining the exact copy of an existing object is called as cloning. There are two types of cloning           · Shallow Cloning: In this type of cloning any modifications to the original object will also modify the cloned object.           · Deep Cloning: In this type of cloning any modifications in the original object will not modify the cloned object.                     Deep cloning or copy can be achieved through serialization. This may be fast to code but will have performance implications. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com . javatbrains . cloning ; class Employee implements Cloneable { int id ; String name ; Employee ( int id , String name ) { this . id = id ;

arrays in java with example

The main definition of an array is, an array represents the group of elements.  In Java, array plays a key role. The main purpose of the array is to hold a fixed set of elements. Why I said fixed set is, while creating array we have to define the size of the array. So, that array can hold only that no of elements in it. For example, when we have created main() method in our program, you can observe there is a String[] array. Otherwise, JVM can't understand and it will not consider that main method as starting point of the application. How to create an array in Java? The below are two different ways of creating an array in java. In the first way, you have to pass the elements between the braces {}.  int [] ar = {}; or int [] ar = new int [ 10 ]; As I have already mentioned, array represents the group of elements. Those elements should be of same type. Otherwise compiler will not allow to add. But, there is a possibility of casting. For example, I have an

Polymarphism interview questions and answers in java

Polymorphism is one of the Oops concept and mostly used feature in java. The ability of an object to take more than one form is called Polymorphism. The most common use of polymorphism in OOPs occurs when a parent class reference is used to refer to a child class object.         It is impossible to know that the only possible way to access the object is through a reference variable. A reference variable can be of  only one type. Once declared, the type of the reference variable cannot be changed.         A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface. Ex: Let's take a look into the below example 1 2 3 4 5 interface Vehicle {} class Mileage {} public class Car extends Mileage implements Vehicle {} see the implementation of the above example, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3

Object Oriented Programming in java

Object Oriented Program or Oops is a technique to create programs based on the real world object. In the Oops programming model programs are organized around objects and data rather than actions and logic. What is Oops?           An Object Oriented Programming organizes a program around its data, i.e., objects and a set of well defined interfaces to that data. An object-oriented programming can be characterized as data controlling access to code. Why Oops?           Object Oriented Programming enables programmers define not only the data, but also its associated behaviors that can be applied to the data. It also enables programmers to create relationship between one object and another. For example, Objects can inherit characteristics from other objects. To understand the importance of Oops first we need to understand the problem with Procedural / Structural programming practices. Problems with Procedure Oriented Programming?           Procedure Oriented Programming