0% found this document useful (0 votes)
29 views

Basics Java

The document discusses Java constructors and methods. It compares and contrasts constructors and methods, explaining that constructors initialize objects while methods perform actions. It also discusses this() and super() usage in constructors, differences between class and instance methods, and access specifiers in Java.

Uploaded by

Harsh mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Basics Java

The document discusses Java constructors and methods. It compares and contrasts constructors and methods, explaining that constructors initialize objects while methods perform actions. It also discusses this() and super() usage in constructors, differences between class and instance methods, and access specifiers in Java.

Uploaded by

Harsh mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Tweet

Your Ac c ount | Adve rtise with us | Fe e dbac k |


Few pages of this site are under construction and we are constantly trying to improve it. If you have any suggestions or ideas about how to improve the site, please Let us know.

Ho me
Core Java Int erview Ques t ions -2 Te st
Home

Art icle s OOPS Inte rvie w Que stions Java Basic Inte rvie w Que stions

Articles Java Exc e ptions Inte rvie w Que stions Java Colle c tions Inte rvie w Que stions
T ut o rials
Java Thre ads Inte rvie w Que stions Java Ge ne ric s Inte rvie w Que stions
Spring Tutorials
Java Annotations Inte rvie w Que stions Java I/O Inte rvie w Que stions
iBat is T ut o rials

JSF-MyFaces Tag Ref. � Pre vious | 1 2 3 4 5 | Ne xt �


JSF Tutorials

Hibernate Tutorials
Java Basic Interview Questions
Struts Tutorials

More....
31. What is Const ruct or?
Int e rvie w Q ue st io ns
A constructor is a special method whose task is to initialize the
Spring Questions object of its class.
It is special because its name is the same as t he class name.
JSF Questions
They do not have return types, not even void and therefore they
Hibe rnat e Q ue st io ns cannot return values.
Struts Questions
They cannot be inherit ed, though a derived class can call the
base class constructor.
AJAX Questions Constructor is invoked whenever an object of its associated class
JSP Questions
is created.

Servlets Questions

JDBC Questions 32. How does t he Java def ault const ruct or be provided?
Core Java Questions If a class defined by the code does not have any constructor,
XML Questions
compiler will automatically provide one no-parameter-constructor
(default-constructor) for the class in the byte code. The access
Webservices Questions modifier (public/private/etc.) of the default constructor is the same
EJB Questions as the class itself.

webMethods Questions
33. Can const ruct or be inherit ed?
Tibco Questions
No, constructor cannot be inherited, though a derived class can call the base class constructor.
BREW Questions

More....
34. What are t he dif ferences bet ween Cont ruct ors and Met hods?
Ce rt ificat io ns
Const ruct ors Met hods
Spring Ce rt ificat io n
Purpose Create an instance of a class Group Java statements
SCJP 6.0
Modifiers Cannot be abstract, final, native, static, or Can be abstract, final, native, static, or
SCJP 5.0
synchronized synchronized
SCJP 1.4
Ret urn T ype No return type, not even void void or a valid return type
SCWCD

SCBCD
Name Same name as the class (first letter is capitalized Any name except the class. Method names
by convention) -- usually a noun begin with a lowercase letter by convention --
SCEA usually the name of an action
SCMAD
this Refers to another constructor in the same class. Refers to an instance of the owning class.
SCDJWS If used, it must be the first line of the constructor Cannot be used by static methods.
J2EE super Calls the constructor of the parent class. If used, Calls an overridden method in the parent class
WebSphere must be the first line of the constructor

XML Inherit ance Constructors are not inherited Methods are inherited
PMP

UML 35. How are t his() and super() used wit h const ruct ors?
webMethods Constructors use this to refer to another constructor in the same class with a different parameter list.
Constructors use super to invoke the superclass's constructor. If a constructor uses super, it must use it in the first line;
Struts
otherwise, the compiler will complain.
More....

36. What are t he dif ferences bet ween Class Met hods and Inst ance Met hods?
Class Met hods Inst ance Met hods
Instance methods on the other hand require an instance of
Class methods are methods which are declared as static.
the class to exist before they can be called, so an instance
The method can be called without creating an instance of
of a class needs to be created by using the new keyword.
the class
Instance methods operate on specific instances of classes.
Class methods can only operate on class members and not Instance methods of the class can also not be called from
on instance members as class methods are unaware of within a class method unless they are being called on an
instance members. instance of that class.
Class methods are methods which are declared as static.
The method can be called without creating an instance of Instance methods are not declared as static.
the class.

37. How are t his() and super() used wit h const ruct ors?
Constructors use this to refer to another constructor in the same class with a different parameter list.
Constructors use super to invoke the superclass's constructor. If a constructor uses super, it must use it in the first line;
otherwise, the compiler will complain.
38. What are Access Specifiers?
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this
class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access
specifiers..

39. What are Access Specifiers available in Java?


Java offers four access specifiers, listed below in decreasing accessibility:
Public- public classes, methods, and fields can be accessed from everywhere.
Prot ect ed- protected methods and fields can only be accessed within the same class to which the methods and fields
belong, within its subclasses, and within classes of the same package.
Def ault (no specifier)- If you do not set access to specific level, then such a class, method, or field will be accessible from
inside the same package to which the class, method, or field belongs, but not from outside this package.
Privat e- private methods and fields can only be accessed within the same class to which the methods and fields belong.
private methods and fields are not visible within subclasses and are not inherited by subclasses.

Sit uat ion public protected def ault private

Accessible to class
yes yes yes no
from same package?

Accessible to class
yes no, unless it is a subclass no no
from different package?

40. What is final modifier?


The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on
whether it is applied to a class, a variable, or a method.
final Classes- A final class cannot have subclasses.
final Variables- A final variable cannot be changed once it is initialized.
final Met hods- A final method cannot be overridden by subclasses.

41. What are t he uses of final met hod?


There are two reasons for marking a method as final:
Disallowing subclasses to change the meaning of the method.
Increasing efficiency by allowing the compiler to turn calls to the method
into inline Java code.

42. What is st at ic 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.

43. What are st at ic variables?


Variables that have only one copy per class are known as static variables.
They are not attached to a particular instance of a class but rather belong
to a class as a whole. They are declared by using the static keyword as a modifier.
static type varIdentifier;

where, the name of the variable is varIdentifier and its data type is specified by type.
Not e: Static variables that are not explicitly initialized in the code are automatically initialized with a default value. The default
value depends on the data type of the variables.

44. What is t he dif ference bet ween st at ic and non-st at ic variables?


A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take
on unique values with each object instance.

45. What are st at ic met hods?


Methods declared with the keyword static as modifier are called
static methods or class methods. They are so called because they
affect a class as a whole, not a particular instance of the class. Static
methods are always invoked without reference to a particular
instance of a class.
Not e:The use of a static method suffers from the following
restrictions:
A static method can only call other static methods.
A static method must only access static data.
A static method cannot reference to the current object using
keywords super or this.

� Pre vious | 1 2 3 4 5 | Ne xt �

ALSO SEE :

About Us | O ur Mission | Terms of use | Author Terms | Se arch | Contact Us | Advertise With Us | Link to Us | Sitemap

Co pyright © 2016 de ve lo pe rsBO O K.CO M. All right s re se rve d.

You might also like