Oops
1. What is Inheritance?
A: Inheritance: When class want to aquaired the properties of another
class we use
the concept of Inheritance.
2. What is the use of Virtual Keyword for a
class?
A: virtual: This keyword Defines a class or method that allows
extension and overrides.
You can’t override a method with the override keyword
unless the class or method has
been defined as
virtual.
(Or)
If you define any class as Virtual it can be Inherited by
another class.
Example:
public virtual class MyException extends Exception {
// Exception class member
// variable
public Double d;
// Exception class constructor
MyException(Double d) {
this.d = d;
}
// Exception class method
protected void doIt() {}
}
Example:
public virtual class MyException extends Exception {
// Exception class member
// variable
public Double d;
// Exception class constructor
MyException(Double d) {
this.d = d;
}
// Exception class method
protected void doIt() {}
}
3.What is the use
of extends keyword?
A. extends: Defines
a class that extents another class.
(or)
If a class want to Inherit another class we use extends
keyword.
Example:
public class MyClass1 extends MyClass {
}
Example:
public class MyClass1 extends MyClass {
}
4. What is
Constructor?
A. Constructor: It is a method with the same name of the class which is
used to
initialize the
data members of the class.
Constructor will not have any return type. Constructor
will be invoked once at
the time of creating an object
Ex: public Class ClassName{
public ClassName(){
}
}
5. How is Constructor Overloading?
A. Constructor Overloading: With in the same class if you write more than one
constructor with a
different no.of parameters, diff type of
parameters we
called it as
“Constructor Overloading”.
Ex: public class
Example{
Public String name;
Public Integer age;
Public Example(){
name=’---***---‘;
age=20;
}
Public Example(String
name){
This.name=name; }
Public
Example(Integer age){
This.age=age;
}
}
6. What is the difference between static and non-static
variable?
A. Static And Non-Static: If you define any variable as Static memory will be
allocated only once
i.e., on the name of class
Memory for the
Non=Static variables will be allocated no.of times based on
objects that we have created.
Static variables are
referred using Class name. Memory for the static variables
will be allocated before the execution starts
Example:
Public Class Example{
Public Static Integer a=0;
Public
Integer b=0;
Public void add(){
a=a+10;
b=b+10;
}
}
7.What are final variables?
A.Final Variables: If you
define any variable as final we cant change the value
after the
creation of object. Values for the final
variable can be assigned at
the
time of creating a
variable or with in the constructor.
Every Object can have its own final value.
Ex: public class Example{
Public
Integer a;
Public
final Integer b;
Public
final Integer c=10;
Public
Example(Integer b){
a=
20;
this.b=
b;
}
}
Debug Log:
Example e1=new Example(25);
Example e2=new
Example(50);
e1.b=90;
// this may give error
e1.a=100;
Useful post, thanks for taking time to share this interview questions with answers. It is really helpful.
ReplyDeleteSalesforce Course in Chennai | Salesforce Training Chennai