0% found this document useful (0 votes)
161 views4 pages

Java Concepts: Constructors, Interfaces, and More

This() can invoke a constructor in the same class, while super() invokes the parent class constructor. A marker interface provides additional information about an object without defining any methods. We can call one constructor from another using the this() method. The second highest number in an array can be found by iterating through the array, tracking the max and second max, and updating second max if a higher number is found.

Uploaded by

harikishore14
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views4 pages

Java Concepts: Constructors, Interfaces, and More

This() can invoke a constructor in the same class, while super() invokes the parent class constructor. A marker interface provides additional information about an object without defining any methods. We can call one constructor from another using the this() method. The second highest number in an array can be found by iterating through the array, tracking the max and second max, and updating second max if a higher number is found.

Uploaded by

harikishore14
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

What is the difference between this() and super()?

this() can be used to invoke a constructor of the same class


whereas super() can be used to invoke a super class constructor.

How can be define MARKER interfce in java

MARKER INTERFACE or TAGGED INTERFACE ARE GIVING SOME SORT


OF INFORMATION ABOUT THAT PARTICULAR OBJECT and it will
giving some marker abt thae object

Generally a interface witout methods are called are


marker/tagged interface..(but this is not corect)

Ex:
clonable,runnable,comparable,serializable

A inrefaces with some ability are ex of marker interface

1)in comparable interface,


one method compare() will be there.......>
.....

ex:
interface Good{}....>gives some infor.abt obj called as it
is good

interface Bad{}

how to call One constructor from another;

Using super you can invoke constructor which is in parent


class. to invoke constructor which is in the same class we
have to use this. for example:

public class Point {


int m_x;
int m_y;

//============ Constructor
public Point(int x, int y) {
m_x = x;
m_y = y;
}

//============ Parameterless default constructor


public Point() {
this(0, 0); // Calls other constructor.
}
. . .
}

write java code to print second max number in the array

public static void main(String[] args){


int maxNumber = 0;
int secondMaxNumber = 0;
if(args.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < args.length; i++){
int currNumber = Integer.parseInt(args[i]);
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max. is "+secondMaxNumber);
}
}

wHAT IS DEFAULT SPECIFIER IN JAVA wHAT IS DEFAULT CONSTRUCTOR IN


JAVA wHAT IS DEFAULT METHOD IN JAVA

default specifier specifies access level of class


eg:-
class A{
}
this is default
default constructor is constructor with same name as of
class and no arguments
eg: class A{
A()
}
there is no default method in java

Can we declare static variables in JSP page.

yes
we should do in jsp declarative tag only.if we specify
variables in declaration tag, it belongs to class.
<%! static int i=0; %>

and u can not define static variable in scriptlet(<% %>).


as we know that we can not declare a static variable in a
method.

What are Advatages of Overloading and Overridding.

Overloading means same functions but different arguments..


so with overloading we can take new action with diff
parameters..

Overriding means same methods with same argument and same


prototype...
so with overriding we can create new definatin in subclass
with same method name and prototype..

In C we use only compiler. Why java uses both compiler and interpreter? What is its
significance?

because in java, code is 1st converted into bytecode by the


compiler nd then it is converted into machine code by the
interpreter.

How to validate the request (Eg:user name and password) in session(http session)? not in
LDAP server ?

You can Use a HashMap to put all user objects into into it
and put this hashmap into session and whenever required get
HashMap from session and get object with key as user name
and then validate it against login

why marker interfaces are there in java

When a class implements a marker interface, then the object


of that class is given with a special behavior. For example
if a class implements remote interface(available in
java.rmi.* package) object of that class acts as a Remote
Object. For this reason java provides Marker Interface.
If all the methods in abstract class are declared as abstract then what is difference
between abstract class and in interface?

if all the methods are abstract in abstract class,then the


only difference left is,,,,

interface can have static and final variables with public


and abstract access modifier only ,,,while abstract class
can have any type of variable with any type of access
modifiers

Why cant we define System.out.println() inside a class directly?

We can't because The method signature matters when you are


declaring the method. we can't give a name with
System.out.println, but we can define println()

public class Garbage { int a=0; public void add() { int c=10+20; System.out.println(c);
System.out.println(a); } public static void main(String args[]) { Garbage obj=new
Garbage(); System.gc(); System.out.println("Garbage Collected"); obj.add(); } } Above is
a code in java used for garbage collection. object obj has been created for the class
Garbage and system.gc method is called. Then using that object add method is
called.System.gc method if called the obj should be garbage collected?

Garbage collector is system created thread which runs


automatically.

We are not sure when the garbage collection is going to


happen. this totally depends upon the JVM. Like connection
pool all the the objects are created in pool JVM will check
if there is no free memory in pool then it searches for the
objects which are no longer in use and will garbage collect
that and allocate to some other object.

Is it compulsory to have atleast one abstract method in abstract class?

yes it is very much compulsory to have a one abstract mthod


in abstract class
otherwise will be no longer abstract

You might also like