Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, 25 February 2017

What is Method Overriding In Java ?

Method Overriding :

It is also known as Run time polymorphism or dynamic binding. In Method Overriding we have more than one methods with same name and with same arguments , but in different class(means here one class extends other class).
In Method Overriding by default super class constructor is available in sub class.

There are some rules to achieved method overriding in Java :
  1. Methods must have same name.
  2. Methods must have same number of argument list.
  3. Method must have same return type.
  4. Only public, protected and default(same package) methods can be overridden.
  5. If method has default access, then overriding method must be default, protected or public.
  6. If method has protected access, then overriding method must be protected or public.
  7. If method has public access, then overriding method must be public.  
  8. Final methods cannot be overridden. Because final is used to declare constant.
  9. Static methods cannot be overridden. Because static method is available to all instance of super class and sub class.So it's not permissible to re-implement the static method in sub class.
  10. Use super keyword to invoke the parent class method from sub class.
  11. Constructor can not overridden.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class ParentClass {
 public ParentClass() {
  System.out.println("Default Parent Class Constructor");
 }
 void show(){
  System.out.println("Parent class Method");
 }
}
public class OverridingExample extends ParentClass {
 void show(){
  System.out.println("Sub class Method");
 }
 public static void main(String[] args) {
  OverridingExample o = new OverridingExample();
  o.show(); //Here Sub Class Method will execute
  
  ParentClass p = new OverridingExample();
  p.show(); //Here also Sub Class Method will execute, because parent class method will override in sub class
 }
}

Output :

Default Parent Class Constructor
Sub class Method
Default Parent Class Constructor
Sub class Method


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is Method Overloading In Java ?

Method Overloading :

It is also known as Compile time polymorphism or static binding ,because in this case compiler easily guess which method is to call, at compile time. In Method Overloading we have more than one methods with same name but with different arguments, in same class.
There are some rules to achieved method overloading in Java :
  1. All methods must have same name.
  2. All methods must not have same method signature(means no two methods have same type/ same number/ same order of arguments).
  3. Methods return type are not part or rule of method overloading.
  4. Thrown exceptions are not part or rule in method overloading.
public class OverloadingExample {
 
 public void sum(int a,int b){
  System.out.println("METHOD 1 : "+(a+b));
 }
 public void sum(Integer a,Integer b){
  System.out.println("METHOD 2 : "+(a+b));
 }
 public void sum(float a,float b){
  System.out.println("METHOD 3 : "+(a+b));
 }
 public void sum(double a,double b){
  System.out.println("METHOD 4 : "+(a+b));
 }
 public static void main(String[] args) {
  OverloadingExample o =new OverloadingExample();
  o.sum(10,10);
  o.sum(new Integer(20),new Integer(10));
  o.sum(10.32f,10.32f);
  o.sum(10.32,10.32);
 }
}

Output:

METHOD 1 : 20
METHOD 2 : 30
METHOD 3 : 20.64
METHOD 4 : 20.64


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Friday, 24 February 2017

What is Polymorphism In Java ?

Polymorphism is a ability to exits in different forms or take more than one form. For example an operation may exhibit different behavior in different instance. The behavior depends upon type of data used in operation. For example addition of two numbers will produce sum ,but if both operands are string then it will produce third string. 
For Example in below figure, single method name can be used to handle different number of arguments and different type of arguments. 
Polymorphism 
In same way Single Objects can also have a different forms like
Object obj = new Object();
Object obj = new String();
Object obj = new Integer(5); 

In below example show() method can accept any type of data ,and it will produce output.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class PolymorphismExample {
 
 public static void show(Object obj){
  if(obj instanceof java.lang.String){
   System.out.println("String Object Found");
  }
  else if(obj instanceof java.lang.Integer){
   System.out.println("Integer Object Found");
  }
  else{
   System.out.println("Undefined Type Object");
  }
 }
 public static void main(String[] args) {
  PolymorphismExample.show("Java");
  PolymorphismExample.show(101.655);
 }
}

Output :
String Object Found
Undefined Type Object

Polymorphism exists in two forms:
1 ) Compile time polymorphism :- It is also known as static polymorphism or binding ,and in java it is achieved by Method Overloading.

2 ) Run time polymorphism :- It is also known as dynamic polymorphism or binding ,and in java it is achieved by Method Overriding.

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Thursday, 23 February 2017

What is Mutilevel Inheritance In Java ?

Multilevel Inheritance :
Multilevel Inheritance

Multilevel Inheritance is derived from derived class ,means a subclass is inherited from another subclass as shown in above figure.
As per below program Class Pen inherits class Book and class Books inherits class Stationary, means class Books is parent class of class Pen and class Stationary is parent class of class Books, so in this case class Pen is inheriting all the properties of class Books and class Stationary ,this is called multilevel inheritance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Stationary{
 void method_A(){
  System.out.println("Stationary CLass Method");
 }
}
class Books extends Stationary{
 void method_B(){
  System.out.println("Book CLass Method");
 }
}
class Pens extends Books{
 void method_C(){
  System.out.println("Pen CLass Method");
 }
}
public class MultilevelInheritanceExample {
 
 public static void main(String[] args) {
  Pens p = new Pens();
  p.method_A();
  p.method_B();
  p.method_C();
 }
}

Output:

Stationary CLass Method
Book CLass Method
Pen CLass Method


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is Hierarchical Inheritance In Java ?

Hierarchical Inheritance :

Java supports Hierarchical Inheritance. In Hierarchical Inheritance there is one super class and many sub classes. for example below figure shows Hierarchical classification of accounts in bank. This is possible because all the accounts have some certain common features. 

 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
41
42
43
44
45
46
47
48
49
50
51
52
53
class Account{ //Parent Class
 void accountMinimumBalance(){
  System.out.println("ACCOUNT MINIMUM BALANCE RS 0/-");
 }
}
class Saving extends Account { // Sub Class of Account 
 void getSavingMinimumBalance(){
  System.out.println("SAVING ACCOUNT MINIMUM BALANCE RS 10/-");
 }
}
class FixedDeposite extends Account { // Sub Class of Account
 void getFixedDepositeMinimumBalance(){
  System.out.println("FIXED-DEPOSITE ACCOUNT MINIMUM BALANCE RS 20/-");
 }
}
class Current extends Account { // Sub Class of Account
 void getCurrentMinimumBalance(){
  System.out.println("CURRENT ACCOUNT MINIMUM BALANCE RS 30/-");
 }
}
class Short extends FixedDeposite { // Sub Class of FixedDeposite
 void getShortMinimumBalance(){
  System.out.println("SHORT ACCOUNT MINIMUM BALANCE RS 40/-");
 }
}
class Medium extends FixedDeposite { // Sub Class of FixedDeposite
 void getMediumMinimumBalance(){
  System.out.println("LONG ACCOUNT MINIMUM BALANCE RS 50/-");
 }
}
class Long extends FixedDeposite{ // Sub Class of FixedDeposite
 void getLongMinimumBalance(){
  System.out.println("MEDIUM ACCOUNT MINIMUM BALANCE RS 60/-");
 }
}
public class HierarchicalInheritanceExample {
 public static void main(String[] args) {
  Account a = new Account();
  a.accountMinimumBalance();
  Saving b = new Saving();
  b.getSavingMinimumBalance();
  FixedDeposite c = new FixedDeposite();
  c.getFixedDepositeMinimumBalance();
  Current d = new Current();
  d.getCurrentMinimumBalance();
  Short e = new Short();
  e.getShortMinimumBalance();
  Medium f = new Medium();
  f.getMediumMinimumBalance();
  Long g = new Long();
  g.getLongMinimumBalance();
 }
}
Output:

ACCOUNT MINIMUM BALANCE RS 0/-
SAVING ACCOUNT MINIMUM BALANCE RS 10/-
FIXED-DEPOSITE ACCOUNT MINIMUM BALANCE RS 20/-
CURRENT ACCOUNT MINIMUM BALANCE RS 30/-
SHORT ACCOUNT MINIMUM BALANCE RS 40/-
LONG ACCOUNT MINIMUM BALANCE RS 50/-
MEDIUM ACCOUNT MINIMUM BALANCE RS 60/-



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Wednesday, 22 February 2017

What is Multiple Inheritance in Java ?

In Multiple Inheritance one class extends more than one class or interface. Java doesn't supports multiple inheritance because multiple inheritance result in diamond problem.
Multiple inheritance means multiple classes/ interface with same methods name and this leads to ambiguity.

What is Diamond Problem ?
In below example as we have four classes i.e A ,B ,C and D ,and classes A, B and C have method with same name and class D extends B and C (In Java class can only extends one class ,if we try to extend more than one class ,then this will leads to compile time exception),now the problem comes with class D ,to override which class method i.e. B or C.
Diamond Problem

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class A{
 
}
class B extends A {
 
}
class C extends A{
 
}
public class D extends B,C { //this leads to syntax error
 
 public static void main(String[] args) {
  
 }
}



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is Single Inheritance in Java ?

In Single Inheritance there is one super class which is extended by the sub class. In below program class Room is extended by BedRoom class ,both classes have their own data members and methods. The constructor in derived class(BedRoom class) uses super keyword to pass values that are required by the base class(Room class) constructor. Detailed program is shown below.

 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
class Room{
 int length;
 int breadth;
 public Room(int x,int y) {
  length = x;
  breadth = y;
 }
 int area(){
  return (length * breadth);
 }
}
class BedRoom extends Room {
 int height;
 public BedRoom(int x, int y, int z) {
  super(x, y);
  height = z;
 }
 int volume(){
  return (length * breadth * height);
 }
}
public class SingleInheritanceExample {
 
 public static void main(String[] args) {
  BedRoom b = new BedRoom(10, 10, 10);
  int area = b.area();
  int volume = b.volume();
  System.out.println("AREA : "+area);
  System.out.println("VOLUME : "+volume);
 }
}

Output:

AREA : 100
VOLUME : 1000



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 21 February 2017

What is Inheritance In Java ?

Inheritance is a mechanism that allow to copy all the features(variables and methods) of super class in sub class. Inheritance exists in different forms:
  1. Single Inheritance(only one super class)
  2. Multiple Inheritance(Many super classes)
  3. Hierarchical Inheritance(one super class and many sub classes)
  4. Multilevel Inheritance(Derived from derived class)
Java supports two ways to implement Inheritance 
  1. extends keyword(used with classes)
  2. implement keyword(used with interfaces)
Java doesn't supports multiple Inheritance, but still this concept can be implemented using interface.

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »