Beruflich Dokumente
Kultur Dokumente
10)What is casting?
Ans: Casting is used to convert the value of one type to another.
15)What is UNICODE?
Ans: Unicode is used for internal representation of characters and strings and it uses
16 bits to represent each other.
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.
Encapsulation
Wrapping of data and function into a single unit called encapsulation. Ex:- all java
programs.
Abstraction
Nothing but representing the essential futures without including background details.
Dynamic binding
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.
27) class & object?
class
◊ class is a Template that describes the Kind of State(The Instance Variables) and
Behavior (Methods)
class is a blue print of an object component means u can use a piece of code like an◊
independent piece.like servlet,EJB…etc
instance of class u can reuse it in any◊Object application
28) System.out.println()
println() is a methd of java.io.printWriter.◊
“out” is an instance variable of java.lang.System class.◊
JIT is a part of JVM, it compiles byte code into executable code in real time, will
increase the performance of the interpretations.
Access Specifier
Access Modifiers
Primitive data types can be converted into objects by using wrapper classes. These are
java.lang.package.
◊ What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error “NoSuchMethodError”.
We can write “static public void” instead of “public static void” but not “public void
static”.◊
40) Constructor
Constructor can use the access specifiers public, protected or private or have no
access modifier (package access)◊
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
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 same class Refers to instance of class
“Super” to invoke the super class constructor Execute an overridden method in the
super class
“Inheritance” cannot be inherited Can be inherited
We can overload but we cannot overridden Can be inherited
Will automatically invoke when an object is created Method has called explicitly
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. In Java on
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.
Final: – When we declare a sub class a final the compiler will give error as “cannot
subclass final class” Final to prevent inheritance and method overriding. Once to
declare a variable as final it cannot occupy memory per instance basis.
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 any
cleanup processing before the object is garbage collected.
Class: A class defines the properties and behavior (variables and methods) that is
shared by all its objects. It is a blue print for the creation of objects. The primitive data
type and keyword void is work as a class object.
Object: Object is the basic entity of object oriented programming language. Class
itself does nothing but the real functionality is achieved through their objects. Object
is an instance of the class. It takes the properties (variables) and uses the behavior
(methods) defined in the class.
The Encapsulation, Inheritance and Polymorphism are main pillars of OOPs. These
have been described below :
Encapsulation:
Encapsulation is the process of binding together the methods and data variables as a
single entity. It keeps both the data and functionality code safe from the outside world.
It hides the data within the class and makes it available only through the methods.
Java provides different accessibility scopes (public, protected, private ,default) to hide
the data from outside. Here we provide a example in which we create a class “Check”
which has a variable “amount” to store the current amount. Now to manipulate this
variable we create a methods and to set the value of amount we create setAmount()
method and to get the value of amount we create getAmount() method .
Here is the code for “Mainclass” class :
class Check{
private int amount=0;
public int getAmount(){
return amount;
}
public void setAmount(int amt){
amount=amt;
}
}
public class Mainclass{
public static void main(String[] args){
int amt=0;
Check obj= new Check();
obj.setAmount(200);
amt=obj.getAmount();
System.out.println(“Your current amount is :”+amt);
}
}
Here the data variable “amount” and methods setAmount() and getAmount() are
enclosed together with in a single entity called the “Check” class. These two methods
are used to manipulate this variable i.e. set and get the current value of amount.
Here is the output of the example:
C:\>javac Mainclass.java
C:\>java Mainclass
Your current amount is :200
Inheritance:
Inheritance allows a class (subclass) to acquire the properties and behavior of another
class (superclass). In java, a class can inherit only one class (superclass) at a time but
a class can have any number of subclasses. It helps to reuse, customize and enhance
the existing code. So it helps to write a code accurately and reduce the development
time. Java uses extends keyword to extend a class.
Here is the code of the example :
class A{
public void fun1(int x){
System.out.println(“Int in A is :” + x);
}
}
class B extends A{
public void fun2(int x,int y){
fun1(6); // prints “int in A”
System.out.println(“Int in B is :” + x ” and “+y);
}
}
public class inherit{
public static void main(String[] args){
B obj= new B();
obj.fun2(2);
}
}
In the above example, class B extends class A and so acquires properties and behavior
of class A. So we can call method of A in class B.
Here is the output of the example:
C:\>javac inherit.java
c:\>java inherit
Int in A is :6
Int in B is :2 and 5
Polymorphism :
Polymorphism allows one interface to be used for a set of actions i.e. one name may
refer to different functionality. Polymorphism allows a object to accept different
requests of a client (it then properly interprets the request like choosing appropriate
method) and responds according to the current state of the runtime system, all without
bothering the user.
There are two types of polymorphism :
1. Compile-time polymorphism
2. Runtime Polymorphism
In compiletime Polymorphism, method to be invoked is determined at the compile
time. Compile time polymorphism is supported through the method overloading
concept in java.
Method overloading means having multiple methods with same name but with
different signature (number, type and order of parameters).
Here is the code of the example :
class A{
public void fun1(int x){
System.out.println(“The value of class A is : ” + x);
}
public void fun1(int x,int y){
System.out.println(“The value of class B is : ” + x + ” and ” + y);
}
}
public class polyone{
public static void main(String[] args){
A obj=new A();
// Here compiler decides that fun1(int) is to be called and “int” will be printed.
obj.fun1(2);
// Here compiler decides that fun1(int,int)is to be called and “int and int” will be
printed.
obj.fun1(2,3);
}
}
Here is the output of the example:
C:\>javac polyone.java
C:\>java polyone
The value of class A is : 2
The value of class B is : 2 and 3
class B extends A{
public void fun1(int x){
System.out.println(“int in Class B is : “+ x);
}
}
C:\>java polytwo
int in Class A is : 2
int in Class B is : 5
In the above program, obj has been declared as A type. In line 1, object of class A is
assigned. Now in the next line, fun1(int) of class A will be called. In line 3, obj has
been assigned the object of class C so fun1(int) of class C will be invoked in line 4.
Now we can understand that same name of the method invokes different functions,
defined in different classes, according to the current type of variable “obj”. This
binding of method code to the method call is decided at run time.