Académique Documents
Professionnel Documents
Culture Documents
Q) Opps concepts
Polymorphism
Ability to take more than one form, in java we achieve this using Method Overloading (compile
time polymorphism), Method overriding (runtime polymorphism)
Inheritance
Is the process by which one object acquires the properties of another object. The advantages of
inheritance are reusability of code and accessibility of variables and methods of the super class
by subclasses.
Encapsulation
Wrapping of data and function into a single unit called encapsulation. Ex:- all java programs.
(Or)
Nothing but data hiding, like the variables declared under private of a particular class are
accessed only in that class and cannot access in any other the class. Or Hiding the information
from others is called as Encapsulation. Or Encapsulation is the mechanism that binds together
code and data it manipulates and keeps both safe from outside interference and misuse.
Abstraction
Nothing but representing the essential futures without including background details.
Dynamicbinding
Code associated with a given procedural call is not known until the time of the call at
runtime. Dynamic binding is nothing but late binding.
Q) Object creation?
Object is constructed either on a memory heap or on a stack.
Memory heap
Generally the objects are created using the new keyword. Some heap memory is allocated to
this newly created object. This memory remains allocated throughout the life cycle of the
object. When the object is no more referred, the memory allocated to the object is eligible to
be back on the heap.
Stack
During method calls, objects are created for method arguments and method variables. These
objects are created on stack.
Q) System.out.println()
println() is a methd of java.io.printWriter.
“out” is an instance variable of java.lang.System class.
Q) Transient & volatile
Transient --> The transient modifier applies to variables only, the object are variable will not
persist. Transient variables are not serialized.
Volatile --> value will be changed unexpectedly by the other part of the program, "it tells the
compiler a variable may change asynchronously due to threads"
Q) Default Values
Q) Byte code & JIT compiler & JVM & JRE & JDK
Byte code is a highly optimized set of instructions. JVM is an interpreter for byte code.
Translating a java program into byte code helps makes it much easier to run a program in a
wide variety of environment.
JVM is an interpreter for byte code
JIT (Just In Time) is a part of JVM, it compiles byte code into executable code in real time, will
increase the performance of the interpretations.
JRE is an implementation of the Java Virtual Machine, which actually executes Java programs.
JDK is bundle of software that you can use to develop Java based software, Tools provided by
JDK is
(i) javac – compiler (ii) java – interpretor (iii) jdb – debugger (iv) javap -
Disassembles
(v) appletviewer – Applets (vi) javadoc - documentation generator (vii) javah - 'C' header
file generator
Q) Wrapper classes
Primitive data types can be converted into objects by using wrapper classes. These are
java.lang.package.
Q) Constructor
The automatic initialization is performed through the constructor, constructor has same
name has class name. Constructor has no return type not even void. We can pass the
parameters to the constructor. this() is used to invoke a constructor of the same class. Super() is
used to invoke a super class constructor. Constructor is called immediately after the object is
created before the new operator completes.
Constructor can use the access modifiers public, protected, private or have no access
modifier
Constructor can not use the modifiers abstract, static, final, native, synchronized or
strictfp
Constructor can be overloaded, we cannot override.
You cannot use this() and Super() in the same constructor.
Class A(
A(){
System.out.println(“hello”);
}}
Class B extends A {
B(){
System.out.println(“friend”);
}}
Class print {
Public static void main (String args []){
B b = new B();
}
o/p:- hello friend
Q) Diff Constructor & Method
Constructor Method
Use to instance of a class Grouping java statement
No return type Void (or) valid return type
Same name as class name As a name except the class method name,
begin with lower case.
“This” refer to another constructor in the Refers to instance of class
same class
“Super” to invoke the super class Execute an overridden method in the super
constructor class
“Inheritance” cannot be inherited Can be inherited
We can “overload” but we cannot Can be inherited
“overridden”
Will automatically invoke when an object Method has called explicitly
is created
Q) Garbage collection
G.C is also called automatic memory management as JVM automatically removes the
unused variables/objects (value is null) from the memory. User program cann't directly free the
object from memory, instead it is the job of the garbage collector to automatically free the objects
that are no longer referenced by a program. Every class inherits finalize() method from
java.lang.Object, the finalize() method is called by garbage collector when it determines no more
references to the object exists. In Java, it is good idea to explicitly assign null into a variable
when no more in use, calling System.gc() and Runtime.gc(), JVM tries to recycle the unused
objects, but there is no guarantee when all the objects will garbage collected. Garbage collection
is a low-priority thread.
G.C is a low priority thread in java, G.C cannot be forced explicitly. JVM may do garbage
collection if it is running short of memory. The call System.gc() does NOT force the garbage
collection but only suggests that the JVM may make an effort to do garbage collection.
Finally: - Finally create a block of code that will be executed after try catch block has completed.
Finally block will execute whether or not an exception is thrown. If an exception is thrown, the
finally block will execute even if no catch statement match the exception. Any time a method is
about to return to the caller from inside try/catch block, via an uncaught exception or an explicit
return statement, the finally clause is also execute.
Using System.exit() in try block will not allow finally code to execute
Finalize: - some times an object need to perform some actions when it is going to destroy, if an
object holding some non-java resource such as file handle (or) window character font, these
resources are freed before the object is going to destroy.
The access modifier for the overriding method may not be more restrictive than the access
modifier of the superclass method.
The throws clause of the overriding method may only include exceptions that can be thrown
by the superclass method, including its subclasses.
Only member method can be overriden, not member variable
class Parent{
int i = 0;
void amethod(){
System.out.println("in Parent");
}
}
class Child extends Parent{
int i = 10;
void amethod(){
System.out.println("in Child");
}
}
class Test{
public static void main(String[] args){
Parent p = new Child();
Child c = new Child();
System.out.print("i="+p.i+" ");
p.amethod ();
System.out.print("i="+c.i+" ");
c.amethod();
}
}
o/p: - i=0 in Child i=10 in Child
Q) Final variable
Once to declare a variable as final it cannot occupy memory per instance basis.
Q) Static block
Static block which exactly executed exactly once when the class is first loaded into JVM.
Before going to the main method the static block will execute.
When a member is declared a static it can be accessed before any object of its class are
created.
Instance variables declared as static are essentially global variables.
If you do not specify an initial value to an instance & Static variable a default value will be
assigned automatically.
Methods declared as static have some restrictions they can access only static data, they can
only call other static data, they cannot refer this or super.
Static methods cant be overriden to non-static methods.
Static methods is called by the static methods only, an ordinary method can call the static
methods, but static methods cannot call ordinary methods.
Static methods are implicitly "final", because overriding is only done based on the type of the
objects
They cannot refer “this” are “super” in any way.
Q) Class variable & Instance variable & Instance methods & class methods
Instance variable variables defined inside a class are called instance variables with multiple
instance of class, each instance has a variable stored in separate memory location.
Class variables you want a variable to be common to all classes then we crate class variables.
To create a class variable put the “static” keyword before the variable name.
Class methods we create class methods to allow us to call a method without creating instance
of the class. To declare a class method use the “static” key word .
Instance methods we define a method in a class, in order to use that methods we need to first
create objects of the class.
Q) Conversions
String to Int Conversion: -
int I = integer.valueOf(“24”).intValue();
int x = integer.parseInt(“433”);
float f = float.valueOf(23.9).floatValue();
Q) Super()
Super() always calling the constructor of immediate super class, super() must always be
the first statements executed inside a subclass constructor.
Member classes - Member inner classes are just like other member methods and member
variables and access to the member class is restricted, just like methods and variables. This
means a public member class acts similarly to a nested top-level class. The primary difference
between member classes and nested top-level classes is that member classes have access to
the specific instance of the enclosing class.
Local classes - Local classes are like local variables, specific to a block of code. Their visibility is
only within the block of their declaration. In order for the class to be useful beyond the declaration
block, it would need to implement a more publicly available interface. Because local classes are
not members the modifiers public, protected, private and static are not usable.
Anonymous classes - Anonymous inner classes extend local inner classes one level further. As
anonymous classes have no name, you cannot provide a constructor.
Q) Abstract Class
Any class that contain one are more abstract methods must also be declared as an
abstract, there can be no object of an abstract class, we cannot directly instantiate the abstract
classes. A.C can contain concrete methods.
Any sub class of an Abstract class must either implement all the abstract methods in the super
class or be declared itself as Abstract.
Compile time error occur if an attempt to create an instance of an Abstract class.
You cannot declare “abstract constructor” and “abstract static method”.
An “abstract method” also declared private, native, final, synchronized, strictfp,
protected.
Abstract class can have static, final method (but there is no use).
Abstract class have visibility public, private, protected.
By default the methods & variables will take the access modifiers is <default>, which is
accessibility as package.
An abstract method declared in a non-abstract class.
An abstract class can have instance methods that implement a default behavior.
A class can be declared abstract even if it does not actually have any abstract methods.
Declaring such a class abstract indicates that the implementation is somehow incomplete and is
meant to serve as a super class for one or more subclasses that will complete the
implementation.
A class with an abstract method. Again note that the class itself is declared abstract, otherwise
a compile time error would have occurred.
Abstract class A{
Public abstract callme();
Void callmetoo(){
}
}
class B extends A(
void callme(){
}
}
class AbstractDemo{
public static void main(string args[]){
B b = new B();
b.callme();
b.callmetoo();
}
}
Q) When we use Abstract class?
A) Let us take the behaviour of animals, animals are capable of doing different things like flying,
digging,
Walking. But these are some common operations performed by all animals, but in a different way
as well. When an operation is performed in a different way it is a good candidate for an abstract
method.
Q) Interface
Interface is similar to class but they lack instance variable, their methods are declared
with out any body. Interfaces are designed to support dynamic method resolution at run time. All
methods in interface are implicitly
abstract, even if the abstract modifier is omitted. Interface methods have no implementation;
Why Interfaces?
“ one interface multiple methods “ signifies the polymorphism concept.
Interface A
{
final static float pi = 3.14f;
}
class B implements A
{
public float compute(float x, float y) {
return(x*y);
}
}
class test{
public static void main(String args[])
{
A a = new B();
a.compute();
}
}
Externalizable is an Interface that extends Serializable Interface. And sends data into
Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and
readExternal(ObjectInput in)
Q) Serialization
Serialization is the process of writing the state of the object to a byte stream, this is useful
when ever you want to save the state of your programme to a persistence storage area.
Q) Synchronization
Synchronization is a process of controlling the access of shared resources by the
multiple threads in such a manner that only one thread can access one resource at a time. (Or)
When 2 are more threads need to access the shared resources they need to some way ensure
that the resources will be used by only one thread at a time. This process which is achieved is
called synchronization.
Q) Monitor
A monitor is a mutex, once a thread enter a monitor, all other threads must wait until that
thread exist the monitor.
(sb1==sb2); F (s1.equals(s2)); T
(sb1.equals(sb2)); F ((s1==s2)); T
(sb1.equals(ss1)); F (s3.equals(s4)); T
((s3==s4)); F
String s1 = "abc";
String s2 = new String("abc");
s1 == s2 F
s1.equals(s2)) T
URL is to identify a resource in a network, is only used to read something from the network.
URL url = new URL(protocol name, host name, port, url specifier)
Q) Runtime class
Runtime class encapsulate the run-time environment. You cannot instantiate a Runtime
object. You can get a reference to the current Runtime object by calling the static method
Runtime.getRuntime()
Runtime r = Runtime.getRuntime()
Long mem1;
Mem1 = r.freeMemory();
Mem1 = r.totalMemory();
Q) Execute other programs
You can use java to execute other heavy weight process on your multi tasking operating
system, several form of exec() method allow you to name the programme you want to run.
Runtime r = Runtime.getRuntime();
Process p = null;
Try{
p = r.exce(“notepad”);
p.waiFor()
}
Q) System class
System class hold a collection of static methods and variables. The standard input,
output, error output of the java runtime are stored in the in, out, err variables.
Q) Native Methods
Native methods are used to call subroutine that is written in a language other than java,
this subroutine exist as executable code for the CPU.
Q) Cloneable Interface
Any class that implements the cloneable interface can be cloned, this interface defines no
methods. It is used to indicate that a class allow a bit wise copy of an object to be made.
Q) Clone
Generate a duplicate copy of the object on which it is called. Cloning is a dangerous
action.
Q) Comparable Interface
Classes that implements comparable contain objects that can be compared in some
meaningful manner. This interface having one method compare the invoking object with the
object. For sorting comparable interface will be used.
Ex:- int compareTo(Object obj)
Q) Class
Class encapsulate the run-time state of an object or interface. Methods in this class are
Q) java.jlang.Reflect (package)
Reflection is the ability of software to analyse it self, to obtain information about the field,
constructor, methods & modifier of class. You need this information to build software tools that
enables you to work with java beans components.
Q) InstanceOf
Instanceof means by which your program can obtain run time type information about an
object.
Ex:- A a = new A();
a.instanceOf A;
Q) Java lack pointers how do I implements classic pointer structures like linked list?
A) Using object reference.
Q) java. Exe
Micro soft provided sdk for java, which includes “jexegentool”. This converts class file into a
“.Exec” form. Only disadvantage is user needs a M.S java V.M installed.