Friday, July 25, 2008
What is the difference between interpreted code and compiled code?
An interpreter produces a result from a program, while a compiler produces a program written in assembly language and in case of Java from bytecodes.The scripting languages like JavaScript,Python etc. require Interpreter to execute them.So a program written in scripting language will directly be executed with interpreter installed on that computer,if it is absent then this program will not execute.While in case of compiled code,an assembler or a virtual machine in case of Java is required to convert assembly level code or bytecodes into machine level instructions/commands.Generally, interpreted programs are slower than compiled programs, but are easier to debug and revise.
What is Java class file's magic number?
A Magic Number of a class file is a unique identifier for tools to quickly differentiate class files from non class files.The first four bytes of each Java class file has the magic value as 0xCAFEBABE.And the answer to why this number,I do not actually know but there may be very few sensible and acceptable options possible constructed from letters A-F which can surely not be 'CAFEFACE' or 'FADECAFE'....
What is heap and stack?
The heap is the part of memory of JVM where all objects reside.
The stack is consisted of stack frames.When a thread invokes a method,the JVM pushes a new frame onto that thread's Java stack.Each stack frame is consisted of operand stack and the local variable array.All arguments,local variables,intermediate computations and return values if any are kept in these stack corresponding to the method invoked.The stack frame on the top of the stack is called the active stack frame,which is the current place of execution.When the method completes, the virtual machine pops and discards the frame for that method.
The stack is consisted of stack frames.When a thread invokes a method,the JVM pushes a new frame onto that thread's Java stack.Each stack frame is consisted of operand stack and the local variable array.All arguments,local variables,intermediate computations and return values if any are kept in these stack corresponding to the method invoked.The stack frame on the top of the stack is called the active stack frame,which is the current place of execution.When the method completes, the virtual machine pops and discards the frame for that method.
How is Java different from C++?
Java is a platform independent, object oriented language while C++ is having some of its features from C, which is a procedural language so it is not pure object oriented. Even java is not 100% object oriented.
1. Pointers are supported in C++ while not in Java. The memory management is done automatically with help of part of JVM called Garbage Collector.
2. Multiple inheritance is not supported in Java but supported in C++.
3. There are no structures, unions or enumeration in Java.
4. There is no scope resolution operator in Java (::).
5. There are no destructors in Java like C++.
6. There is no virtual keyword in Java because all non-static method use dynamic binding.
1. Pointers are supported in C++ while not in Java. The memory management is done automatically with help of part of JVM called Garbage Collector.
2. Multiple inheritance is not supported in Java but supported in C++.
3. There are no structures, unions or enumeration in Java.
4. There is no scope resolution operator in Java (::).
5. There are no destructors in Java like C++.
6. There is no virtual keyword in Java because all non-static method use dynamic binding.
What is method overriding?
The method with same signature but with changed implementation lead to method overriding and that can occur in a parent child relation of classes. A method defined in parent class can be overridden in its child class with different implementation from its base class.
An example:
We will define a base class called Circle
class Circle
{
//declaring the instance variableprotected double radius;
public Circle(double radius)
{
this.radius = radius;
}
// other method definitions here
public double getArea()
{
return Math.PI*radius*radius;
}
//this method returns the area of the circle
}// end of class circle
When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.
The next step is to define a subclass to override the getArea() method in the Circle class. The derived class will be the Cylinder class. The getArea() method in the Circle class computes the area of a circle, while the getArea method in the Cylinder class computes the surface area of a cylinder.
The Cylinder class is defined below.
class Cylinder extends Circle
{
//declaring the instance variableprotected double length;
public Cylinder(double radius, double length)
{
super(radius);this.length = length;
}
// other method definitions herepublic
double getArea()
{
// method overriden here
return 2*super.getArea()+2*Math.PI*radius*length;
}//this method returns the cylinder surface area
}// end of class Cylinder
When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass(Circle).
An example:
We will define a base class called Circle
class Circle
{
//declaring the instance variableprotected double radius;
public Circle(double radius)
{
this.radius = radius;
}
// other method definitions here
public double getArea()
{
return Math.PI*radius*radius;
}
//this method returns the area of the circle
}// end of class circle
When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.
The next step is to define a subclass to override the getArea() method in the Circle class. The derived class will be the Cylinder class. The getArea() method in the Circle class computes the area of a circle, while the getArea method in the Cylinder class computes the surface area of a cylinder.
The Cylinder class is defined below.
class Cylinder extends Circle
{
//declaring the instance variableprotected double length;
public Cylinder(double radius, double length)
{
super(radius);this.length = length;
}
// other method definitions herepublic
double getArea()
{
// method overriden here
return 2*super.getArea()+2*Math.PI*radius*length;
}//this method returns the cylinder surface area
}// end of class Cylinder
When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass(Circle).
What is method overloading?
A method with changed formal parameters will lead to implementing method overloading.
int calculateSum(int i,int j)
float calculateSum(float i,int j)
double calculateSum(double i,int j)
float calculateSum(int i,float j)
int calculateSum(int i,int j)
float calculateSum(float i,int j)
double calculateSum(double i,int j)
float calculateSum(int i,float j)
Why is Java not 100% pure OOP language?
Java takes inspirations from C and C++.The native datatypes like 'char','int','float','double' are straight pick from C, which is not an Object Oriented Language.Resonably enough, Java is not a 100% pure Object Oriented Language.
Why does Java not support Multiple Inheritance?
Java does not support multiple inheritance atleast not the way it does in case of C++.In designer’s view Multiple Inheritance poses many problems and confusions than it solves.e.g. famous Diamond problem The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common super class. In such scenarios assuming if Java implements multiple inheritance then it would be difficult to know which method is to be called by an inheriting class object of two of the super classes. In Java, interfaces solve all these ambiguities caused by the diamond problem. Through interfaces, Java allows multiple inheritance of interface but not of implementation. Implementation, which includes instance variables and method implementations, is always singly inherited. As a result, confusion will never arise in Java over which inherited instance variable or method implementation to use.
Why Java uses Singly rooted hierarchy?
All objects in Java are inherited from same base class called 'Object'.In Java all objects have common interface to implement and it makes implementaion of Garbage collector lot easier in Java.The necessary implementaion is provided in base class , and the garbage collector can then send the necessary messages to every objectc in the system.Without singly rooted hierarchy,it would have been difficult to implement garbage collection feature.It enables lot of ease to programmers not to be bothered about memory management while development.It greatly simplifies argument passing amongst object too on the heap. As Java started from scratch and has no backward compatibility issues with any existing language, it was a logical choice to use the singly-rooted hierarchy in common with most other object-oriented programming languages.
What is the difference between Data Abstraction and Information Hiding?
Data Abstraction is often confused with information hiding while they altogether are two different technical concepts.Here are few established definitions of Data Abstraction:
"A view of a problem that extracts the essential information relevant to a particular purpose and ignores the remainder of the information." -- [IEEE, 1983]
"The essence of abstraction is to extract essential properties while omitting inessential details." -- [Ross et al, 1975]
"Abstraction is the selective examination of certain aspects of a problem. The goal of abstraction is to isolate those aspects that are important for some purpose and suppress those aspects that are unimportant." -- [Rumbaugh et al, 1991]
"An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer." -- [Booch, 1991]
While information hiding is not sharing the details of an object with outside world.Here are few standard definitions of Information Hiding which will elaborate more on this concept:
"The technique of encapsulating software design decisions in modules in such a way that the module's interfaces reveal little as possible about the module's inner workings; thus each module is a 'black box' to the other modules in the system." -- [IEEE, 1983]
"The process of hiding all the details of an object that do not contribute to its essential characteristics; typically, the structure of an object is hidden, as well as the implementation of its methods. The terms information hiding and encapsulation are usually interchangeable." -- [Booch, 1991]
"The principle of information hiding is central. It says that modules are used via their specifications, not their implementations. All information about a module, whether concerning data or function, is encapsulated with it and, unless specifically declared public, hidden from other modules." -- [Graham, 1991]
"A view of a problem that extracts the essential information relevant to a particular purpose and ignores the remainder of the information." -- [IEEE, 1983]
"The essence of abstraction is to extract essential properties while omitting inessential details." -- [Ross et al, 1975]
"Abstraction is the selective examination of certain aspects of a problem. The goal of abstraction is to isolate those aspects that are important for some purpose and suppress those aspects that are unimportant." -- [Rumbaugh et al, 1991]
"An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer." -- [Booch, 1991]
While information hiding is not sharing the details of an object with outside world.Here are few standard definitions of Information Hiding which will elaborate more on this concept:
"The technique of encapsulating software design decisions in modules in such a way that the module's interfaces reveal little as possible about the module's inner workings; thus each module is a 'black box' to the other modules in the system." -- [IEEE, 1983]
"The process of hiding all the details of an object that do not contribute to its essential characteristics; typically, the structure of an object is hidden, as well as the implementation of its methods. The terms information hiding and encapsulation are usually interchangeable." -- [Booch, 1991]
"The principle of information hiding is central. It says that modules are used via their specifications, not their implementations. All information about a module, whether concerning data or function, is encapsulated with it and, unless specifically declared public, hidden from other modules." -- [Graham, 1991]
What is Data Encapsulation?
Data Encapsulation is wrapping informations(attributes and behaviors) within an object.A suitable example is a class as it wraps methods and data within itself. The attributes of a class corresponds to its data members while behaviour corresponds to member methods of the class.
Thursday, July 24, 2008
Expain the reason for each keyword of public static void main(String args[])?
public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.
static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.
void: main does not return anything so the return type must be void
The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.
Tuesday, July 22, 2008
What is a Class?
A class is a collection of attributes and behaviors of objects with certain similarities and an instance of a class is represented by an object.A simple example of class is a 'Car' which represents variety of Car objects with different attribute values and behaviors.The different objects of 'Car' class can be, for example : A Mercedes Car,a Toyota Car, two different objects from same class but different attributes and different behaviors too.
What is an Object?
An object is an entity with certain attributes or qualities and behaviors, for a simple example, a 'Laptop' is an object which has certain attributes like weight,color,screen size,manufacturer etc.It has various behaviors or activities to do or act upon, as play games,browse Internet,write/check emails , watch movies ,listen music etc.
Subscribe to:
Posts (Atom)