on Leave a Comment

Functional Dependeny in DBMS

Functional dependency in DBMS is a constraint between two set of attributes. In a relational database schema having n attributes, there exists a universal relation R = {A1, A2, A3, ..., An}. A functional dependency is denoted by X→Y, where X and Y are subset of R. In relation R, attribute (or set of attributes) Y is functionally dependent to attribute X, means value of X can uniquely determine the value of Y.

The abbreviation of functional dependency is FD or f.d.
The left hand side is called determinant and right hand side is dependent.

Consider the below relation, in this each value of X uniquely determines value of Y


Now consider the below table, here there are two 1s in X attribute but both determines two different value of Y, this violates functional dependency X->Y.



Inference Rules for Functional Dependencies

In a database schema, F is a set of functional dependency. But are some other dependency that can be inferred or deduced from functional dependencies FD in F. 

The set of all dependencies including F as well as other dependencies inferred from F are known as closure of F and it is denoted by F*

For example: {Emp_id → {Emp_name, Emp_salary, Emp_mobile}}

Emp_mobile → {Emp_name, Emp_salary}

Following dependencies can be inferred from above two FD

Emp_mobile → Emp_name
Emp_id → Emp_id  

The following rules are inference rules for functional dependency.  

- Reflexive rule 

If Y ⊆ X, then X →Y

- Augmentation rule

If {X →Y},  then  XZ →YZ

- Transitive rule

If  {X →Y, Y →Z}, then X →Z

- Decomposition, or projective, rule

If {X →YZ}, then X →Y

- Union, or additive, rule

If {X →Y, X →Z}, then X →YZ 

- Pseudotransitive rule

If {X →Y, WY →Z}, then WX →Z




on Leave a Comment

Data Redundancy in DBMS with example

Data redundancy means redundant copies of data within database. Data redundancy leads to data inconsistency which will cause overall database performance. To avoid redundancy problem in database design Normalization is used. 

Opensource DBMS mysql provides some ways to avoid redundancy such as joins.

Example of data redundancy in DBMS

Data redundancy in DBMS

In the above table, Dep_id and Dep_manager_name is copied into multiple tuples . For example, Dep_id 22 is placed two times in table. If we need to update Dep_manager_name for Dep_id 22, then we have to update all records having Dep_id 22. If Dep_id 22 is placed with thousand records, then we need to update thousand record for small change and this results to increase in computational time.

Disadvantages of data redundancy in DBMS

Insertion anomaly

If there is one more Dep_id, let say 21 and currentlty no employee is working in this department. Then there will be no entry related to this Dep_id in table. Such a situation is known as insertion anomaly.

Deletion anomaly

In the above table, if record Emp_code 3 is deleted, then there will no record related to Dep_id 17. This will cause losing information about Dep_id 17.   

Updation anomaly

If we need to change Dep_manager name, then we have to update to record having same Dep_id. This will increase computation time.


Advantages of data redundancy in RDBMS

But sometimes, data redundancy is solution when you have unlimited storage and your basic need is faster access of data. When we store data in multiple tables for example student information in STUDENT table and student courses in another table STUDENT_COURSE and we need to access complete student data in one query, then joins is solution. But when data is large enough, joins decrease performance. There are many other solutions for faster join operation like indexing and other database constraint. But if data is large and joins are not working well, then data redundancy is also a solution.  



on 1 comment

Get Video Information PHP | Embed/Embed Package

PHP embed/embed package can used used for fetching video information such as duration , thumbnail and many other useful information.

First install the package via composer 

composer require embed/embed 

Then, include autoloader.php in source file and fetch video information as follows.

<?php

include "../Embed/src/autoloader.php";

use Embed\Embed;

$vidDetail =  getVideoDetail("<your video url>"); //it can be embed link also

function getVideoDetail($url){
    //Load any url:
   $info = Embed::create($url);
   $duration = "";
   foreach ($info->getProviders() as $providerName => $provider){
   foreach($provider->getBag()->getAll() as $prKey=>$prVal){
   if($prKey == duration){
   $duration = $prVal;
   continue;
   }
   }
   if($duration) continue;
   }
   if(strpos($url, "youtube.com")){
    $duration = str_replace("PT", "", $duration);
    $duration = str_replace("S", "", $duration);
    $durationArr = explode("M", $duration);
            $duration = $durationArr[0] * 60 + $durationArr[1] - 1;
   }
   $thumbnail = $info->image;
   return array("thumbnail" => $thumbnail, "duration" => $duration);
}

?>


on 1 comment

JavaScript Program to Display Hello World

In JavaScript, you can display any string using alert box. JavaScript alert is a method of window object. But we can also it explicitly without reference to window object. There are many other ways in JavaScript to display string like console.log method.

JavaScript Program to Display Hello World using Alert Box

<!doctype html>
<html>
  <head>
      <meta charset="utf-8">
      <title>Hello World</title>
  </head>
  <body>
      <button onclick="print()">Click Me...</button>
  </body>
  <script>
      function print(){
          alert("Hello World...");
      }
  </script>
</html>

Output:



When you click on this button, a pop up box occurs that displaying "Hello World...".

JavaScript Program to Display Hello World using console.log

console.log method is used to print string on console window. If you use this method nothing will display on the screen, but it will display on console window.

<!doctype html>
<html>
  <head>
      <meta charset="utf-8">
      <title>Hello World</title>
  </head>
  <body>
      <button onclick="print()">Click Me...</button>
  </body>
  <script>
      function disp(){
          console.log("Hello World...");
      }
  </script>
</html>

Output:







on Leave a Comment

JavaScript Variables

As other programming languages, JavaScript also supports data types and variables. Variables are piece of memory blocks in which we can store data (values). And data types decides what type of data can be stored in variables and what operations on variable can be performed.

JavaScript supports following data types:

Number, e.g. 100, 105.5, 10.0 etc.
String, e.g. 'abc', "xyz" etc.
Boolean, e.g. true and false 
Null and Undefined
Object (composite data type)

JavaScript represents numbers as 64-bit floating-point format defined by the IEEE 754 standard.

Example of JavaScript Variable

var keyword is used to declare variables in JavaScript

<script type="text/javascript">
      var a = 10;
      var b = 20;
      var c = a + b;
</script>

In above example, we define three variable using var keyword namely a, b and c. Variables a and b stores 10 and 20 respectively. And sum of a and b is assigned to variable c.

JavaScript Identifiers

All names are Identifiers. A variable must have unique name.

- Reserved words cannot be used as a variables name.
- Variables name should not start with a number like 0, 1, 2.
- Variable name should start with a alphabets or underscore like abc, _abc.
- All variable names are case sensitive.








on Leave a Comment

Introduction to Javascript

Javascript is a client side scripting language. It is a primary used language on the web. It is lightweight and interpreted language. Javascript has ability to change HTML content. Today all modern web browsers support javascript. 

Javascript is developed by Netscape Communication at 1995. Javascript was first known as liveScript and then it changed to Javascript, may be because of popularity of Java. 

Advantage of Javascript

It is client side scripting language. Javascript code runs by the user processer and thus it saves bandwidth interaction with server.

Javascript is easy to learn and it’s much look like English language. 

Javascript has plenty of predefined function, variable thus provides greater functionality to the user.

Using javascript you can create interactive web pages that catches attraction of site visitor.

It is fast because it runs on user system and its need not be process on web server.

Limitations of Javascript

Javascript code can be used by hackers to infect user's system, so it is security issue. Modern web browsers set some restriction but still, it’s not fully secure.

Using javascript, you can't work with files on server because it is a client side scripting language.

Javascript doesn't support multithreading.

Javscript depends on browser, so it is interpreted differently on different browsers.

All browsers has option to disable javascript, so user can disable javascript. 



on Leave a Comment

Java Program to Find Duplicate Characters in a String

This java program finds duplicate characters in a string. In this program, we assign a string to a variable named "str" and this string is converted to character array using toCharArray() method and assigned to a variable "c". Then using nested for loop, we compare each character of char array to the other characters in array and if they match, we print that character.

Java Program to Find Duplicate Characters in a String

class FindDuplicateString{
 public static void main(String[] args){
  String str = "java";
  char[] c = str.toCharArray();
  System.out.print("Duplicate characters : ");
  for(int i=0; i<str.length(); i++){
   for(int j=i+1; j<str.length(); j++){
    if(c[i] == c[j]){
              System.out.print(c[i]+" ");         }
   }   
  }
 }
}

Output:

Duplicate characters : a



on Leave a Comment

Java Program to Convert String to Int

In java, we can convert string to integer using static methods like parseInt() and valueOf(). 

Java String to Int Using Integer.parseInt()

parseInt() is a static method of Integer class, this method returns a primitive int.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  int a = Integer.parseInt(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110

Java String to Int Using Integer.valueOf()

valueOf() method returns a Integer object.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  Integer a = Integer.valueOf(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110




on Leave a Comment

Java Program to Compare Two Strings

In java, we can compare two strings using some predefined methods namely, equals() and equalsIgnoreCase() method. Strings can also be compared using == operator.

Java program to compare strings by equals() method

public class CompareString{
   public static void main(String []args){
      String s1 = "pctechnicalpro";
      String s2 = "pctechnicalpro";
      String s3 = "Pctechnicalpro";
      String s4 = new String ("pctechnicalpro");
   
      //compare s1 with s2
      System.out.println(s1.equals(s2));
   
      //compare s1 with s3
      System.out.println(s1.equals(s3));
   
      //compare s1 with s4
      System.out.println(s1.equals(s4));
   }
}

Output:

true
false
true

equals() method return true if sequence of characters is same in both string objects, otherwise it returns false.

Java program to compare strings by equalsIgnoreCase() method

public class CompareString{
   public static void main(String []args){
      String s1 = "pctechnicalpro";
      String s2 = "pctechnicalpro";
      String s3 = "Pctechnicalpro";
      String s4 = new String ("pctechnicalpro");
   
      //compare s1 with s2
      System.out.println(s1.equalsIgnoreCase(s2));
   
      //compare s1 with s3
      System.out.println(s1.equalsIgnoreCase(s3));
   
      //compare s1 with s4
      System.out.println(s1.equalsIgnoreCase(s4));
   }
}

Output:

true
true
true

equalsIgnoreCase() method ignore the case and returns true if both string objects have same sequence of character, otherwise it returns false.






on Leave a Comment

Java Program to Find ASCII value of a Character

ASCII(American Standard for Information Interchange) is a code to represent English characters using numeric code. It includes upper and lower case English letters, numbers and punctuation symbols. It assigns a number to each character from 0 to 127. For example ASCII code for alphabet A is 65. In Java, we can find ASCII value of character by assigning a character value to integer variable.

Java Program to Find ASCII Value of Character

public class ShowAscii {

    public static void main(String[] args) {

        char ch = 'A';
        int asciiCode = ch;
        // we can also cast char to int
        int castascii = (int)ch;

        System.out.println("ASCII value of "+ch+" is: " + asciiCode);
        System.out.println("ASCII value of "+ch+" is: " + castascii);
    }
}

Output:

ASCII value of A is: 65
ASCII value of A is: 65


on Leave a Comment

Linear Search in Java

Linear search is a searching technique to search an element in array. We can also search element in array using binary search. Linear search is slower than binary search, so it is less used. But in linear search we don't need to sort an array while performing searching. So both searching technique have their own advantages and disadvantages.

In linear search, we traverse an array for searching specific element. If array is found, we note its index number and if item is not found till end, we display that "Element is not found".

Java program for linear search

/*Linear Search in Java*/
import java.util.Scanner;
public class LinearSearch{
 public static void search(int arr[], int item){
  int i, flag = 0;
  for(i = 0; i<arr.length; i++){
   if(arr[i] == item){
    flag = 1;
    System.out.println("Element "+arr[i]+" is found at index "+i);
   }
  }
  if(flag!=1)
   System.out.println("Element "+arr[i]+" is not found");
 }
 public static void main(String[] args){
  Scanner read = new Scanner(System.in);
  int []arr = new int[5];
  System.out.print("Enter Array Element (5 Elements): ");
  for(int i = 0; i<arr.length; i++){
   arr[i] = read.nextInt();
  }
  System.out.print("Enter the element to search: ");
  int item = read.nextInt();
  search(arr, item);
 }
}

Output:

Enter Array Element (5 Elements): 1 2 3 4 5
Enter the element to search: 3
Element 3 is found at index 2




on Leave a Comment

Binary Search in Java

Binary Search is a simple approach to search an element in array. If you are using binary search for searching element in array, then array must be sorted in ascending order. We can sort an array using sorting algorithms or by using Arrays.sort(arr) method. Binary search is faster than linear search. Binary search is also known as Half-Interval algorithm.

In binary search, we compares the given element with the middle element of array. If middle element is equal to given element then index of the middle element is returned. If given element is less than middle element, the algorithm again repeats on the sub-array to the left of the middle element and if the given element is greater than middle element, the algorithm again repeats on the sub-array to the right of the middle element.

Binary Search in Java

class BinarySearch{
	public static void binarySearch(int arr[], int start, int end, int key){
		int mid = (start + end)/2;  
		while( start <= end ){
			if ( arr[mid] < key ){
				start = mid + 1;     
			}
			else if ( arr[mid] == key ){
				System.out.println("Element "+key+" is found at index: " + mid);  
				break;  
			}
			else{
				end = mid - 1;
			}
			mid = (start + end)/2;  
                }  
		if ( start > end ){
			System.out.println("Element is not found!");  
		}  
	}  
	public static void main(String args[]){  
        int arr[] = new int[]{10,20,30,40,50};  
        int key = 40; 
        int end = arr.length-1;  
        binarySearch(arr,0,end,key);     
 }
}

Output:

Element 40 is found at index: 3

Binary Search in Java Using Recursion

class BinarySearch{
	public static int binarySearch(int arr[], int start, int end, int key){  
        if (end>=start){  
            int mid = start + (end - start)/2;  
            if (arr[mid] == key){  
            return mid;  
            }  
            if (arr[mid] > key){  
            return binarySearch(arr, start, mid-1, key);//search in left subarray  
            }
			else{  
            return binarySearch(arr, mid+1, end, key);//search in right subarray  
            }  
        }  
        return -1;  
    }  
    public static void main(String args[]){  
        int arr[] = {10,20,30,40,50};  
        int key = 50;  
        int end=arr.length-1;  
        int result = binarySearch(arr,0,end,key);  
        if (result == -1)  
            System.out.println("Element is not found!");  
        else  
            System.out.println("Element "+key+" is found at index: "+result);  
    }  
}

Output:

Element 50 is found at index: 4

Binary Search in Java Using Arrays.binarySearch()

binarySearch() is a method in Arrays class, so we need to import java.util package.

import java.util.Arrays;  
class BinarySearch{  
    public static void main(String args[]){  
        int arr[] = {10,20,30,40,50};  
        int key = 20;  
        int result = Arrays.binarySearch(arr,key);  
        if (result < 0)  
            System.out.println("Element "+key+" not found!");  
        else  
            System.out.println("Element "+key+" is found at index: "+result);  
    }  
}

Output:

Element 20 is found at index: 1






on Leave a Comment

What is the World Wide Web (WWW)?

The World Wide Web is a repository of information linked together through hyperlinks all over the world. As Internet have many services and World Wide Web is one of them. The World Wide Web supports special document that are formatted in HTML. Tim Berners-Lee, an English scientist, invents WWW in 1989. Berners-Lee developed hyperlinks to link web pages. User can easily access these web pages through WWW repository.

The WWW is distributed client server service, in which a client access web pages by web browser. These webpages is stored on server. The web browser sends requests to the server for specific web page using URL. After receiving the request, the server sends requested web pages and then these web pages is displayed on browser screen, it is a short description. 

on Leave a Comment

Packages in Java

Package is a way of grouping a variety of classes and interfaces together. In java, package is a way to organize set of related classes and interfaces. In simple words, a package is similar to different folders in our computer. We keep images in one folder, songs in other folder and so on. So, we keep set of related documents in one folder. There can be two classes in two different package can have same name as two files having same name in two different folders in our computer.

In java, we have ability to reuse code by extending classes and implementing interfaces. But if we have to reuse the code written in another program without copying the code. This can be achieved by using packages. Java provides a class library for use in our own application called API or Application Programming Interface. This API provides large number of classes grouped into different packages according to their functionality.

Advantages of Package in Java

(1) While making a large project, we have many classes and interfaces, so it is required to group set of related classes and interfaces into packages. It provides better organisation and improves efficiency.

(2) We can put two classes of same name in two different package, so it avoids name collision.

(3) Java package provides reusability of code by using different classes and interfaces in different packages.

(4) We can't directly access the code of classes and interfaces, so it provides access protection.

Types of Package in Java

Built-in package

Java already provides many different packages like java.lang.*, java.io.* known as built-in packages.

User defined package

We can also create our own package known as user defined package.

Example of Java Package

package showMe;

public class Show{
 public void disp(){
  System.out.println("Hello");
 }
}

import showMe.Show;

public class Demo{
 public static void main(String[] agrs){
  Show obj = new Show();
  obj.disp();
 }
}

Output:

Hello

In this example, first we create a package namely "showme" using package keyword. We define a class called "Show" and this class containing only one method called "disp". While putting a class to a package, the class must be of public access modifier.

We can create another class called "Demo", this class imports "showMe" using import keyword. In Demo class, we create an object of "Show" class of "showMe" package and called a method called "disp".

To Compile: javac -d . Show.java
Then again, javac Demo.java
To run Demo class: java Demo

Example 2:

We can create a class inside a package while importing another package.

package Package2;
import showMe;

class Example{
 public static void main(String[] args){
 Show obj = new Show();
 obj.disp();
 }
}

In this program, first we write package declaration and then we import another package.

It is important to know that if we write package declaration statement in our program, then it must be the first statement of program.

Example 3:

If you don't want to write import statement, then you can use fully qualified name.

Greatest.java

package Package1;
public class Greatest{
  public void checkGreatest(int a, int b){
    if(a>b)
   System.out.println(a+" is greater than "+b);
 else
   System.out.println(b+" is greater than "+a); 
  }
}

Example.java

class Example{
 public static void main(String[] args){
 Package1.Greatest obj = new Package1.Greatest();
 obj.checkGreatest(10, 20);
 }
}

Output:

20 is greater than 10

Sub package in Java

We can create a package inside another package. For example, java has also sub packages like lang, io, net etc.

Example:

Show.java

package Package1.SubPackage1;
public class Show{
 public void disp(){
  System.out.println("Welcome");
 }
}

Example.java

import Package1.SubPackage1.*;
class Example{
 public static void main(String[] args){
 Show obj = new Show();
 obj.disp();
 }
}

Output:

Welcome








on Leave a Comment

Java Program to Check Whether Two Strings are Anagram or Not

This java program checks whether two strings are anagram or not. Anagram means producing a new word using rearranging of letters of a word, length of letters must be same in both new and previous words. In this java program, user will enter two strings and the code of checking anagram runs and show whether both strings are anagram or not. 

Java Program Check Anagram or Not

Example 1:

In this program, first we check the length of both strings, if length is same then runs the if blocks, otherwise run the else block. In else block, print "Strings are not Anagram". And in the if block, we take first letter of first string and compare it with all letters of second string. If the letter not matches, print "Not Anagram" and exit the program. After checking first letter of first string, check another letter. When all letter string 1 matches with string 2, prints "Strings are Anagram".

import java.util.Scanner;
public class CheckAnagram{
	public static void main(String[] args) {
		String str1, str2;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First String: ");
		str1 = sc.next();
		System.out.print("Enter Second String: ");
		str2 = sc.next();

		int i, j, found=0;
		if(str1.length() == str2.length()){
			for(i=0; i<str1.length(); i++)
			{
				for(j=0; j<str1.length(); j++){
					if(str1.charAt(i) == str2.charAt(j)){
						found =1;
					}
				}
				if(found==0){
					System.out.println("Strings are not Anagram");
					System.exit(0);
				}		
			}
			System.out.println("Strings are Anagram");	
		}
		else{
			System.out.println("Strings are not Anagram");
		}	
	}
}

Output:

Enter First String: triangle
Enter Second String: integral
Strings are Anagram

Enter First String: alpha
Enter Second String: beta
Strings are not Anagram

Example 2:

In this program, first we check the length of both strings and if length is same if block runs, otherwise else block runs. In else block, print "Strings are not Anagram". And in if block, sort both array and directly compares string 1 with string 2. If both strings matches print "Strings are Anagram", otherwise print "String are not Anagram".

import java.util.Scanner;
import java.util.Arrays;
public class CheckAnagram{
	public static void main(String[] args) {
		String str1, str2;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First String: ");
		str1 = sc.next();
		System.out.print("Enter Second String: ");
		str2 = sc.next();
        
        char c1[] = str1.toCharArray();
        char c2[] = str2.toCharArray();
        str2 = str2.replaceAll("\\s", "");
        Arrays.sort(c1);
        Arrays.sort(c2);
		if(str1.length() == str2.length()){
			if(Arrays.equals(c1, c2))
				System.out.println("Strings are Anagram");
			else
				System.out.println("Strings are not Anagram");
		}
		else{
			System.out.println("Strings are not Anagram");
		}	
	}
}

Output:

Enter First String: triangle
Enter Second String: integral
Strings are Anagram

Enter First String: alpha
Enter Second String: beta
Strings are not Anagram




on Leave a Comment

Left Rotation of Array in Java

In this article, w will see how to rotate elements of array on left  hand side.

First, we ask the user to enter length of array and number of left rotation. For example, if user enters 3 left rotation for [1, 2, 3, 4, 5] then output will be [4, 5, 1, 2, 3]. Length and number of left rotation can't be a floating point number.

Steps:

Here is length of array and is number of left rotation of array.

- Store the first array element in temp variable.
- Shift the array elements on left side by

for(i=0; i<n-1; i++){
   arr[i] = arr[i+1];
}

- Then assign the temp value to arr[n-1]
- Repeat above steps r times.

Java Program to Left Rotation of Array

import java.util.Scanner;
public class LeftRotation{
 public static void main(String[] args){
  int temp, i, r, j, n;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter Length of Array: ");
  n = sc.nextInt();
  System.out.print("Enter Number of Left Rotation Array: ");
  r = sc.nextInt();
  int[] arr = new int[n];
  System.out.print("Enter Array Elements: ");
  for(i=0; i<n; i++)
   arr[i] = sc.nextInt();
  for(j=0; j<r;j++){
   temp = arr[0];
   for(i=0; i<n-1; i++){
    arr[i] = arr[i+1];
   }
   arr[n-1]=temp;
  }
  System.out.print("Elements are ");
  
  for(int a:arr){
  System.out.print(a+" ");
  }
  
 }
}

Output:

Enter Length of Array: 5
Enter Number of Left Rotation Array: 3
Enter Array Elements: 1 2 3 4 5
Elements are 4 5 1 2 3



on Leave a Comment

Scanner Class in Java

Scanner class is used to read input from keyboard. Scanner is class in java.util package. Scanner class provides many methods to read and parse primitive values. Scanner breaks the input into tokens using delimiter which is by default whitespaces. 

To read input from keyboard, we have to make an object of Scanner class by passing predefined object called System.in.

Example:

Scanner sc = new Scanner(System.in);

Here sc is object of Scanner class and System.in represents the standard input stream.

In java.util package, Scanner class extends Object class and implements Iterator and Closeable interfaces.

Methods in Scanner Class

There are many methods in Scanner class but most commonly used methods are:

1. next() is used to take single word input
2. nextInt() is used to take integer input
3. nextFloat() is used to take float input
4. nextDouble() is used to take double input
5. nextLong() is used to take long input
6. nextShort() is used to take short input
7. nextLine() is used to take string input
8. nextBoolean() is used to take boolean input

next().charAt(0). is used to read a single character.

Example of Java Scanner Class

import java.util.Scanner;
class ScannerExample{
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);

  System.out.print("Enter a String : ");
  String s = sc.nextLine();
  System.out.println("You have entered "+s);

  System.out.print("\nEnter Integer Value : ");
  int a = sc.nextInt();
  System.out.println("You have entered "+a);

  System.out.print("\nEnter Character Value : ");
  char c = sc.next().charAt(0);
  System.out.println("You have entered "+c);
 }
}

Output:


Enter a String : Java
You have entered Java

Enter Integer Value : 10
You have entered 10

Enter Character Value : B
You have entered B



on 1 comment

Multiple Inheritance in Java

Multiple inheritance is a concept of object oriented programming. In multiple inheritance, a class inherits the properties of two or more class. But java does not support multiple inheritance because of some issues. When both parent classes overrides the same method and child class object is calling that method, then compiler does not know which class method is to be called and which class to give high priority.
Multiple Inheritance
Multiple Inheritance

It will show error because one class trying to extending two classes.

// First Parent class
class ParentClass1
{
    void show()
    {
        System.out.println("ParentClass1");
    }
}
 
// Second Parent Class
class ParentClass2
{
    void show()
    {
        System.out.println("ParentClass2");
    }
}
class SubClass extends ParentClass1, ParentClass2{
 public static void main(String[] args) {
  SubClass obj = new SubClass();

  obj.show();
 }
}

Output:

SubClass.java:18: error: '{' expected
class SubClass extends ParentClass1, ParentClass2{
                                   ^
1 error

Multiple Inheritance by Interfaces

In java, one class can implements two or interfaces. This also does not cause any ambiguity because all methods declared in interfaces are implemented in class.

Example:

interface Interface1
{
   public void show();
}
interface Interface2
{
   public void show();
}
class SubClass implements Interface1, Interface2
{
   public void show()
   {
       System.out.println("A class can implements more than one interfaces");
   }
   public static void main(String args[]){
    SubClass obj = new SubClass();
    obj.show();
   }
}

Output:

A class can implements more than one interfaces


on Leave a Comment

OSI Model: Application Layer and its Functions

In OSI model, application layer is the top most layer. The application layer user interface to enable user to access the network, user may be either human or software. Application layer contains communication protocols and interface to perform process to process communication on network.  Application layer acts as the user interface responsible for displaying received information to the user. Application layer supports many services such as electronic mail, remote file access and transfer, shared database system, directory services, network resource etc.

Functions of Application Layer

Network Virtual Terminal: Network virtual terminal allows user to log on to a remote host. Application creates a software emulation of a terminal at the remote host. User communicates with the software terminal which in turn communicates with the host and vice versa. The remote host assumes that it is communicating with one of its own terminals and allows user to log on.

File Transfer, Access and Management: The application layer allow user to access files in a remote host. User can make changes in files and read data from the file. 

Mail Services: The application layer provides services like E-mail forwarding and storage.

Directory Services: It provides distributed database sources and access for global information about various services.





on Leave a Comment

Java Program to Open Notepad

This java program opens notepad by Runtime class. Notepad is an application comes with Windows operating system. Notepad is text editor. In this program, we create an object of Runtime class and calling exec() method by passing parameter as application name. 

Java Program to Open Notepad

import java.util.*;
import java.io.*;
 
class OpenNotepad {
  public static void main(String[] args) {
   System.out.println("Opening Notepad...");
    Runtime rt = Runtime.getRuntime();
 
    try {
      rt.exec("notepad");
    }
    catch (IOException e) {
      System.out.println(e);
    }   
  }
}

Output:

Opening Notepad...

Java Program to Open Notepad
Notepad