Tuesday, 5 April 2016

Inheritance :


Inheritance is one such concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes.

In Java, there are two classes:
  • Parent class ( Super or Base class)
  • Child class (Subclass or Derived class )
A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

Inheritance defines IS-A relationship between a Super class and its Sub class. extends and implements keywords are used to describe inheritance in Java.


Inheritance is further classified into 4 types:
  • Single Inheritance 
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
Note : Multiple inheritance is not supported in java

1.Single Inheritance :




2.Multilevel Inheritance:



3.Multiple Inheritance : 

Having more than one Parent class at the same level is called multiple inheritance.

  • Any class can extends only one class at a time and can’t extends more than one class simultaneously hence java won’t provide support for multiple inheritance.
  • But an interface can extends any number of interfaces at a time hence java provides multiple inheritance support through interfaces.

4.Hierarchical Inheritance :



5.Hybrid Inheritance : 



Tuesday, 22 March 2016

Difference between Hash Set and Hash Map

Ques : What is Difference between Hash Set and Hash Map?

                        Hash Map                       
 Hash Set
HashMap  is a implementation of Map interface
HashSet is an implementation of Set Interface
HashMap Stores data in form of key value pair
HashSet Store only objects
Put method is used to add element in map
Add method is used to add element is Set
In hash map hashcode value is calculated using key object
Here member object is used for calculating hashcode value which can be same for two objects so equal () method is used to check for equality if it returns false that means two objects are different.
HashMap is faster than hashset because unique key is used to access object
HashSet is slower than Hashmap

Wednesday, 16 March 2016

Remove white space from String

package com.tutorial4u;

import java.util.Scanner;

public class RemoveWwhiteSpace {
       public static void main(String[] args) {
             System.out.println("Enter any string : ");
             Scanner sc = new Scanner(System.in);
             String str = sc.nextLine();
             char[] c = str.toCharArray();
             StringBuffer sb = new StringBuffer();
             for(int i=0; i<c.length; i++){
                    if((c[i] !=' ')&& (c[i] !='\t')){
                           sb.append(c[i]);
                    }
             }
             System.out.println(sb);
       }


}

Output:

Enter any string :
Welcome To Tutorial4u
WelcomeToTutorial4u

Interview Question


Q : In how many ways you can create string objects in java?

There are two ways to create string objects in java. One is using new operator and another one is using string literals. The objects created using new operator are stored in the heap memory and objects created using string literals are stored in string constant pool.

1
2
String s1 = new String("ashish"); //Creating string object using new operator
String s2 = "ashish";  //Creating string object using string literal

In case of new operator two object is created one is in heap and other one is in string constant Pool and object is pointing to heap object.

Q: What is difference between String and String Buffer ?

String
String Buffer
 String class are immutable.
 String Buffer class are mutable.
Object of String class are created using both new operator and string literal.
Object of  String Buffer class is created using only new operator .
In String class equals() and hash code method are override. 
 In String Buffer  equals() and hash code method are not override.
In Case of String consume more memory.
 In case of String Buffer consume less memory.
In case of String object are stored String constant pool as well as heap memory.
 In case of String Buffer object are stored only in heap memory

Sunday, 27 December 2015

Basic Oops concept

Basic OOP Concepts

The main Object-Oriented Programming (OOP) concepts are:
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

1. Inheritance

Inheritance is a concept where one class can acquire the properties and behaviors of another class. It helps reuse code and establish a relationship between classes.

In Java, there are two types of classes involved in inheritance:

  • Parent class (also called Superclass or Base class) – the class whose properties are inherited.
  • Child class (also called Subclass or Derived class) – the class that inherits properties from the parent.

A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

๐Ÿ‘‰Read more about Inheritance here.

Example :
class Parent { void show() { System.out.println("Parent class method"); } } class Child extends Parent { void display() { System.out.println("Child class method"); } }

2. Polymorphism :

Polymorphism means “many forms.” It is the ability of a variable, method, or object to take on multiple forms.

In Java, polymorphism is of two types:

  1. Compile-time polymorphism – achieved through method overloading.
  2. Run-time polymorphism – achieved through method overriding.

Example ;
class Parent { void show() { System.out.println("Parent class method"); } } class Child extends Parent { void display() { System.out.println("Child class method"); } }
3. Encapsulation :

Encapsulation is the process of wrapping data and methods together as a single unit. It also involves hiding data to prevent unauthorized access or modification.

In Java, encapsulation is achieved by:

  • Declaring variables as private.
  • Providing public getter and setter methods to access and modify these variables
Example:
class Person {
    private String name;  // private variable
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}  


4. Abstraction :

Abstraction is the process of hiding implementation details and showing only the functionality.

In Java, abstraction is achieved using:

  • Interface – provides 100% abstraction (all methods are abstract).
  • Abstract class – provides 0–100% abstraction (can have both abstract and concrete methods).

Example ;
abstract class Vehicle {
    abstract void start(); // abstract method
}

interface Drivable {
    void drive(); // abstract method
}




Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...