Search This Blog

Monday 15 September 2014

Why Multiple Inheritance is not supported in Java.?

Inheritance:  When an object or class is based on another object or class, using the same implementation (inheriting from a class) or specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy.


1:  class SuperClass{  
2:    void superClassMethod(){  
3:      //Code  
4:    }  
5:  }  
6:  class SubClass extends SuperClass{  
7:    void subClassMethod(){  
8:      //Code  
9:    }  
10:  }  

In the above code, SubClass extends SuperClass and inherits the properties of SuperClass.

A class basically has following:
1. State
2. Field

Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). 
Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Now suppose you are able to inherit more than one class in Java.
Classes-
1. Animal
2. Mammal
3. Bird
4. Dog
Mammal and Bird inherited Animal class which is possible in Java. A class can be inherited by any number of classes if it allowed to be inherited. Dog class is actually a animal (is-a relationship). 

Dog can extend only one class according to Java Language specifications, which means a Dog can acquire the properties of only one class directly. 
Now suppose if your class Dog is capable of inheriting two classes- Mammal and Bird at a time. And when you instantiate a Dog class, the object of Dog will inherit the state and behavior of all the extended superclasses.  Just imagine this example in real world, what if you see a Dog who is flying in above you?

This problem of inheriting properties of more than one class at a time is also known as Diamond Problem
As in the above figure on left you can see, Animal class is extended by two classes - Mammal and Bird which is possible. Now the a dog class extending two classes - Mammal and Bird.

Method and constructor of superclasses accessing/defining the same field as method and constructor of subclasses. Which method or constructor will take precedence? We can't decide the precedence.
This is the reason why multiple inheritance is not there in Java.

There are people out there who say- Interfaces are meant to achieve multiple inheritance. Interfaces is not actually meant for achieving multiple inheritance.
But yes, we can simulate the behaviour of Multiple Inheritance using interface.
Because interfaces do not contain fields and because of this you do not have to worry about problems that result from multiple inheritance of state.

Friday 31 January 2014

Changing the TextColor of Console in Code::Blocks

Hello Programmers,
In this post you will learn about changing the Color of Text on Console using Code::Blocks IDE and MinGW Compiler.
Here we are using a function sytem() which is defined in stdlib.h header file.
The function system() can be used in following two ways:
  • Passing a color code just to change the foreground of the text.
    Syntax:           system("COLOR <color code>");
    Example:        system("COLOR 4"); 
  • Passing two color codes, one for background and one for foreground of the console.
    Syntax:           system("COLOR <color codeF><color codeB>");
                           color codeF is for foreground and
                           color codeB is for background.
    Example:        system("COLOR AC"); 



  1. /*
  2. Aditya's Console Color Changing Code Example
  3. Compiled On:    MigGW (CODE::BLOCKS)
  4. This code will change the Foreground color of the console text.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. int main()
  9. {
  10.     system("COLOR 6");
  11.     printf("Welcome to the COLOUR changing Console Application!\n");
  12.     return 0;
  13. }

Using system("COLOR 6");


       /*Code to change Foreground and Background of console.*/
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {

  5. /* 
    Along with COLOR you can pass two codes 

    Here I have used F for Foreground and C for Background


    */ 
  6.     system("COLOR FC");
  7.     printf("Welcome to the COLOUR changing Console Application! \nThis code changes ForeGround and BackGround");
  8.     return 0;
  9. }


    Using system("COLOR FC")
Once you have understood the concept of using system("COLOR <color code>"); try to play with the given color codes.


/*
Color Codes:
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
*/

Friday 28 June 2013

First Step towards JDBC!

First Step towards JDBC
Introduction
This article introduce you with JDBC and shows you how to create a database application to access the databases. For the shake of simplicity, in very first example Access database and Sun's JDBC-ODBC drivers are used. In the later sections we will show you how to use JDBC from your servlets or JSP pages to create dynamic pages or to store the data received from visitors.
What is JDBC?
Java Database Connectivity or JDBC for short is set of Java API's that enables the developers to create platform and database independent applications in java. The biggest advantage of programming in Java is its platform independence. An application written to access the MS Access database on Win 95/Win NT platform can work on Linux against Oracle database, only by changing the name of driver, provided none of the database calls it makes are vendor specific.
What are JDBC Drivers?
JDBC Drivers are set of classes that enables the Java application to communicate with databases. Java.sql that ships with JDK contains various classes for using relational databases. But these classes do not provide any implementation, only the behaviours are defined. The actual implementaions are done in third-party drivers. Third party vendors implements the java.sql.Driver interface in their database driver.

JDBC Drivers Types
Sun has defined four JDBC driver types. These are:
  1. Type 1: JDBC-ODBC Bridge Driver
    The first type of JDBC dirver is JDBC-ODBC Bridge which provide JDBC access to any ODBC complaint databases through ODBC drivers. Sun's JDBC-ODBC bridge is example of type 1 driver.
  2. Type 2: Native -API Partly - Java Driver
    Type 2 drivers are developed using native code libraries, which were originally designed for accessing the database through C/C++. Here a thin code of Java wrap around the native code and converts JDBC commands to DBMS-specific native calls.
  3. Type 3: JDBC-Net Pure Java Driver
    Type 3 drivers are a three-tier solutions. This type of driver communicates to a middleware component which in turn connects to database and provide database connectivity.
  4. Type 4: Native-Protocol Pure Java Driver
    Type 4 drivers are entirely written in Java that communicate directly with vendor's database through socket connection. Here no translation or middleware layer, are required which improves performance tremendously.

JSF PAGE STRUCTURE

A JSF page has extension .xhtml.

It starts with DTD (Document Type Definition).

                                                      Figure- DTD Example

A DTD contains information about the tags to be used in the whole documents. There is some sort of validation stuff from where the document can be validated.
Then next thing that it contains is the list of libraries so that we can use them in out code.


                                          Figure- JSF Page Example

Figure-  Output of Above Code
As you can see in the above figure that there are few tags in the code. Each of them has some specific meaning and all the user code for a JSF page will be written between them.
For writing code in JSF you need to know the basics of HTML because most of the things in JSF is about HTML only.
You can view following link to see how to create a JSF application.


Enjoy Learning :)

Monday 19 November 2012

Operating System Viva Questions

Operating System Viva Questions


Q.1 What is operating System?
Operating system is a set of programs which provides interface between the user and the hardware.

Q.2 Types of Operating System?
Classes of Operating System:-
       i. Batch Processing Systems
       ii. Multi Programming Systems
       iii. Times Sharing Systems
       iv. Real Time Systems
       v. Distributed Systems

Q.3 Difference between Multi-tasking and Multi-Programming?
Multi-Programming is a concept where CPU switches to another job after completion the first job. In Multi-Programming main memory contains number of jobs simultaneously.

Multi-Tasking is logical extension of Multi-Programming where CPU switches from job to job in a particular time interval.

Q.4 What is paging? Why it is used?
Paging is a memory management scheme. In Paging, each process consists of fixed-size components called pages. The size of a page is defined by the hardware of a computer. Paging is used to solve the memory allocation problem for processes.

Q.5 What is fragmentation? What are its types?
Fragmentation occurs in dynamic memory allocation system when many of the free blocks are too small to satisfy a request.
Types of fragmentation:-

       i. External Fragmentation
       ii. Internal Fragmentation

Q.6 What is Turnaround time, Response time, waiting time and through put?
Turnaround time: The time from submission of a job to the time when results become available to user.
Response time: The time when the process gets first response from the system.
Waiting time: The amount of time a process has been waiting in the ready queue.
Throughput: The average number of jobs, programs, processes subrequests completed by a system in unit time.

Q.7 What is context switch?
The task of saving the current state of the process on PCB (Process Control Block) and restoring or resuming some another process. Context switch may be because of some interrupts (Hardware/Software Failure).

Q.8 What are the reasons for process termination?
A process usually terminates when it finishes its task.
There may few other reasons as well:
a) When the task assigned to the process is no longer required.
b) When parent process is exiting and operating system doesn’t allow the child process to continue its work.

Q.9 What are process states?

New: The process is being created.

Running: Instructions are being executed.

Waiting: The process is waiting for some event to occur (such as an I/O completion or reception of a signal).

Ready: The process is waiting to be assigned to a processor.

Terminated: The process has finished execution.


Q.10 What is thread?
An execution of a program that uses the resource of a process is called thread.

Q.11 What is the difference between thread and process?
A thread differs from process in various ways few of them are as follows:

a) A process can contain more than on thread.
b) No resource is allocated to a thread.
c) Processes are heavily dependent on system resources but a thread requires minimal amount of resources.
d) Threads can easily communicate easily while process can’t communicate so easily.
e) Threads are easy to create but processes are not that state forward.

Q.12 What is spooling and buffering?
Buffering is a method of overlapping the computation of a job with its execution. It temporarily stores input or output data in an attempt to better match the speeds of two devices such as a fast CPU and a slow disk drive. If, for example, the CPU writes information to the buffer, it can continue in its computation while the disk drive stores the information.

With spooling, the disk is used as a very large buffer. Usually complete jobs are queued on disk to be completed later. A typical example is the spooler for a printer. When a print job is issued, the spooler takes care of it, sending it to the printer if it is not busy, or storing it on disk otherwise.

The main difference between buffering and spooling is that the latter allows the I/O of one job to overlap the computation of another. Buffering only allows the I/O of a job to overlap with its own computation.

Q.13 What is a system call?
System calls allow user-level processes to request services of the operating system.

System calls provide an interface between the process and the operating system. System calls allow user-level processes to request some services from the operating system which process itself is not allowed to do. In handling the trap, the operating system will enter in the kernel mode, where it has access to privileged instructions, and can perform the desired service on the behalf of user-level process.

Q.14 What are different types of scheduling?
Scheduling can be classified in following types:-

· Long term scheduling: which determines which programs are admitted to the system for execution and when, and which ones should be exited.

· Medium term scheduling: which determines when processes are to be suspended and resumed;

· Short term scheduling (or dispatching): which determines which of the ready processes can have CPU resources, and for how long.

Taking into account the states of a process, and the time scale at which state transition occur, we can immediately recognize that dispatching affects processes

running;
ready;
blocked;

the medium term scheduling affects processes

ready-suspended;
blocked-suspended;

the long term scheduling affects processes

new;
exited

Q.15 How system detect thrashing?
It can be detected by evaluating the level of CPU utilization as compared to the level of multiprogramming.

Q.16 What is booting?
When we start our Computer then there is an operation which is performed automatically by the Computer which is known as Booting.

Q.17 What is Process Control Block?
A process in an operating system is represented by a data structure known as a process control block (PCB) or process descriptor. The PCB contains important information about the specific process including-
       · The current state of the process i.e., whether it is ready, running, waiting, or whatever.
       · Unique identification of the process in order to track "which is which" information.
       · A pointer to parent process.
       · Similarly, a pointer to child process (if it exists).
       · The priority of process (a part of CPU scheduling information).
       · Pointers to locate memory of processes.
       · A register save area.
       · The processor it is running on.

The PCB is a certain store that allows the operating systems to locate key information about a process. Thus, the PCB is the data structure that defines a process to the operating systems.

Q.18 What is interrupt? When it occurs and its types?
When a Process is executed by the CPU and when a user Request for another Process then this will create disturbance for the Running Process. This is also called as the Interrupt.

Interrupts can be generated by User, Some Error Conditions and also by Software’s and the hardware’s.

Types of Interrupts
Generally there are three types o Interrupts those are Occurred For Example
1) Internal Interrupt
2) Software Interrupt.
3) External Interrupt.

Q.19 What is synchronization?
Synchronization refers to one of two distinct but related concepts: synchronization of processes, and synchronization of data. Process synchronization refers to the idea that multiple processes are to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action. Data synchronization refers to the idea of keeping multiple copies of a dataset in coherence with one another, or to maintain data integrity. Process synchronization primitives are commonly used to implement data synchronization.

Q.20 What is deadlock? When does it occur?
Deadlock is a situation in which processes wait for other processes’ action indefinitely.
User processes share a computer system’s resources. If a resource requested by some process Pi is currently allocated to process Pj, Pi has to wait until Pj releases the resource. Such waits sometimes cause a deadlock.

Q.21 What are the Condition of Deadlock detection, prevention and avoidance?
Deadlock handling approaches:-

Deadlock detection- The kernel analyzes the resource state to check whether a deadlock exists. If so, it aborts some process(es) and allocates the resources held by them to other processes so that the deadlock ceases to exist.

Deadlock Prevention- The kernel uses a resources allocation policy that ensures that the four conditions of resource deadlocks (Non-shareable resources, No preemption, Hold and Wait, Circular waits) do no arise simultaneously. It makes deadlock impossible.

Deadlock Avoidance- The kernel analyzes the allocation state to determine whether granting a resource request can lead to a deadlock in the future. Only request that can’t lead to a deadlock are granted, others are kept pending until they can be granted. Thus, deadlock do not rise.

Q.22 What is page fault and how it occurs?
In memory hierarchy when RAM needs some data, it search for particular page in page memory and if that page is not found then occurs a fault which is called page fault.
Page fault occurs when desired page is not found in page memory.

Q.23 How many types of schedulers?
Long term scheduler or job scheduler and Short term scheduler or CPU scheduler.

Q.24 What are the election algorithms?
When coordinator stops responding to the request of processes then process assumes that coordinator has failed and initiates election algorithm. The election algorithm chooses the highest-priority non failed process as a new coordinator and announces its id to all non failed processes.

Q.25 What is kernel?
Kernel is the core of OS. It controls operations of the computer and provides a set of functions and services to use the CPU, memory, and other resources of the computer.

Q.26 What is inter-process communication?
Inter-process communication (IPC) is a mechanism which allows cooperation processes to exchange and share data while their execution.

There are two fundamental models-
· Message Passing
· Shared Memory


Q.27 What is critical section problem?
A critical section is a section of code when a common variable or data is to be accessed by a set of variable or processes.
When such cooperating processes access such common variable or data, the process is said to be in critical section which usually creates a problem.

Q.28 What is starvation?
Starvation is a situation in which a process indefinitely waits for a resource.

Q.29 What is a safe state in deadlock?
A state is said to be safe if a system can allocate a resource to each process in some order and still avoid deadlock.

Q.30 What is a semaphore?
A semaphore is hardware or a software tag variable whose value indicates the status of a common resource. Its purpose is to lock the resource being used. A process which needs the resource will check the semaphore for determining the status of the resource followed by the decision for proceeding.

Q.31 What is dispatcher?
Dispatcher is a component involved in CPU scheduling. Dispatcher is a module that gives control of CPU to the process selected by a scheduler for swapping, context switching and resuming a user program.

Q.32 What is RPC?
RPC stands for Remote Procedure Call.
A process calls a procedure that is located in different computer system. It invokes a procedure that is executed in another computer in the distributed system.