Friday, 3 November 2017

method overloading in java example


Method Overloading in Java


If a class has multiple methods having same name but different in parameters, this is known as method overloading.

Method overloading increase the readability of the program. We can achieve method overloading either by changing the number of arguments or by changing the data type.

We will explain the method overloading by an example. This example achieve method overloading by changing the data type of argument.

Example 1:

package keywords;



public class Overloading {

       public int sum(int a, int b){

              return a+b;

       }

       public double sum(double a, double b){

              return a+b;

       }

       public static void main(String []p){

              Overloading obj=new Overloading();

              int c=obj.sum(10, 15);

              double k=obj.sum(15.5, 12.7);

              System.out.println("Sum of two integers is "+c);

              System.out.println("Sum of two double number is "+k);

             

       }



}



Output:

Sum of two integers is 25

Sum of two double number is 28.2



Code Explanation:


In this example we have two method in a class complete with same name but these two methods have different type of parameters. When we call the sum method compiler identify the method by the type of parameters passed during the calling of method. When we call sum method with integers parameters then sum method with integer parameter is called, When we call with double parameter then it call method with double parameter. This thing we can observe in the output clearly

Saturday, 28 October 2017

Computer network introduction


What is a Network?




A network is simply defined as something that connects things together for

a specific purpose. The term network is used in a variety of contexts,

Including telephone, television, computer, or even people networks.

A computer network connects two or more devices/computers together to share a nearly limitless range of information and services, including:



·        Documents

·        Email and messaging

·        Websites

·        Databases

·        Music



In below diagram Ok and An connected through a network.







Now come to the Point Protocol:--



Protocols are rules that govern how devices communicate and share


information across a network. Examples of protocols include:



1. IP – Internet Protocol

2. HTTP - Hyper Text Transfer Protocol



Multiple protocols often work together to facilitate end-to-end network

Communication. Protocols are covered in great detail later .

















Basic Types of Network:--




Network types are often defined by function or size or no of connected workstations. The two most common categories of networks are:



·        LANs (Local Area Networks)

·        WANs (Wide Area Networks)





A LAN is generally a high-speed network that covers a small geographic

area, usually contained within a single building or campus. A LAN is

usually under the administrative control of a single organization. Ethernet is the most common LAN technology.





A WAN can be defined one of two ways. The book definition of a WAN is a

network that spans large geographical locations, usually to connect multiple

LANs.





A MAN (Metropolitan Area Network) is another category of network,

though the term is not prevalently used. A MAN is defined as a network that

connects LAN’s across a city-wide geographic area.





An internetwork is a general term describing multiple networks connected

together. The Internet is the largest and most well-known internetwork.











Network Architectures


A host refers to any workstation that is connected to a network. A host can also

 defined as any device assigned an IP address.

A host can use following functions:

• A host which request data, called as a client.

• A which provide data, called as a server.

• A host can both request and provide data, called as a peer.

Because of these functions, multiple network architectures have

been developed, including:



1.     Peer-to-Peer

2.     Client/Server

3.     Mainframe/Terminal



In a basic peer-to-peer architecture, all hosts on the network can both

request and provide data and services. For example, two Windows 10

workstations configured to share files would be considered a peer-to-peer

network.

Peer-to-peer networks are very simple to configure like static routes, yet this architecture presents several challenges. Data is difficult to manage and back-up, as it is spread across multiple workstation. Security is equally problematic, as user

accounts and permissions much be configured individually on each host.



Explanations No 1:--



In a client/server architecture, systems are assigned specific roles. Clients

request data and services stored on servers. An example of a client/server

network would be Windows 10 workstations accessing files off of a

Windows 2007 server.



Advantage of the client/server:--



There are several advantages to the client/server architecture. Data and

services are now centrally located on one or more servers, consolidating the

security and management of that data. As a result, client/server networks can

scale far larger than peer-to-peer networks.



disadvantage of the client/server:--

One key disadvantage of the client/server architecture is that  server can

present a single point of failure. This can be mitigated by adding redundancy at the server layer.

interface in Java


interface keyword


An interface is a keyword in java, it is used to declare interface in java. Like a class, interface can have methods and variables, but the methods declared in interface must be abstract. It is used to achieve abstraction and multiple inheritance in java. Methods in an interface are implicitly public.

Writing interface similar to writing a class. But class describes the attributes and behaviours of an object. And an interface contains behaviours that a class implements.  

Syntax to declare interface:


interface <interface-name>{

//abstract methods

}

Example:

interface Add{

public int add( );

}

How to implement interface


A class uses implement keyword to implement an interface. If class does not define all the methods of interface then the class must be declared as abstract.

Now we will explain how to implement an interface via this following example.

Example

File: Sum.java

interface Add{

public int add( int a, int b);

}

class Complete implements Add{

public int add(int a, int b){

return a+b;

}

}

public class Sum{

public static void main(String [ ]q){

Complete obj=new Complete();

int c=obj.add(5,12);

System.out.println("Sum of  two numbers is "+c);

}

}

Output:

Sum of two numbers is 17

Code explanation:

In this program First we created one interface named as Add and then after we implement this interface by Complete class and this Complete class define the method that is declared inside interface. Now we create third class named as Sum and this class contains main method. Inside this main method we create object of Complete class and via this object we access the method and get the value of addition of two numbers. After getting the addition of two numbers we print that value using System.out.println( ).

Note:

Since java 8, interface can have default and static methods.

Friday, 27 October 2017

abstract keyword in java


What is abstract keyword


abstract keyword is used to implement the abstraction in java. A method which does not have method definition must be declared as abstract. You can't instantiate abstract classes. Abstract methods must be implemented in the sub classes. We can't use abstract keyword with variables and constructors.

What is abstract class


An abstract class is a class that is declared as abstract. It may or may not be include abstract method. If a class has at least one abstract method, then the class must be declared as abstract class. abstract classes cannot be instantiated , but they can be extended. If we extended an abstract class, we have to provide implementation to all the abstract methods in it.

What is abstract method


An abstract method is a method that is declared without an implementation.

Example 1:

public abstract int add(int a, int b);

An abstract method must always be declared in an abstract class or in other word we can say that if a class has an abstract method, it should be declared as abstract.



Now we will take one example to explain abstract class and abstract method

Example 2:

Filename: Sum.java

abstract class Add{

abstract public int add(int a, int b);

}

class Complete extends Add{

public int add(int a, int b){

return a+b;

}

}

public class Sum{

public static void main(String [ ]p){

Complete obj=new Complete( );

int c=obj.add(5,12);

System.out.println("addtion of two numbers is"+c);

}

}



Output of the program:

addition of two numbers is 17



Code Explanation:

In above program first we created an abstract class named as Add and it contains one abstract method add and then we created a class named as Complete that inherits the Add class. Complete class provide implementation to the abstract method of Add class. Now we create third class named as Sum. Inside this main class we create object of Complete class and by using the object we call the add method and it returns sum of the given values and we print the value.