10 | 03 | 2010
Main Menu
Affiliates
Login Form



Alexa
JoomlaWatch Stats 1.2.6 by Matej Koval
Interview Question in java on Classes, Interfaces and Methods
Written by Java Guru   
Sunday, 18 May 2008 03:31
Article Index
Interview Question in java on Classes, Interfaces and Methods
Page 2# Classes, Interfaces and Methods
Page 3# Classes, Interfaces and Methods
Page 4# Classes, Interfaces and Methods
All Pages

Rating 2.5/5 (6 votes)

Q: Differentiate between an Interface and an Abstract class?

A: Interface can contain only constants and public method declarations which should be implemented by classes inheriting it, where as an Abstract class can contain concrete methods (methods that are defined) and abstract methods which are not implemented. A class can implement multiple interfaces but can extend only one abstract class.

Q: When to use Abstract class and interface?

A: Interfaces are used when there are more frequent chances of change in design and you have to extend some class. Abstract classes are used when certain set of default behavior is required and subclasses are forced to provide others. When abstract classes are used, the classes extending the abstract class inherit the behavior of the abstract class which makes it tightly coupled. Interfaces are often used with composition where a class holds reference to an interface type, the actual implementation of the interface can be switched dynamically making the design more flexible.

Q: Can a method call an abstract method and why?

A: Yes, Abstract classes are used when some of the functionality has to be defined in the abstract class itself and some of the functionality has to be delegated to the sub classes. Abstract classes cannot be instantiated, so to use an abstract class we have to extend it and provide implementation to all the abstract methods. This way when a abstract method is invoked, it will be invoked on a concrete implementation of the abstract class.

Q: How does a constructor differ from a method?

A: . Constructors have same name as that of the class and have no return type. Constructors are called when new objects have to be created. They cannot be called as an when just for doing some processing but, but normal methods are called for doing some processing. If no constructor is defined java will provide a default constructor for the class. The method must contain a return type or void if it is returning nothing.

Q: Define pass by reference and pass by value? How are they supported in java?

A: . Pass by reference means we are passing the address itself not its value .Passing by value we passing the copy of the value.

In java only pass by value is available. In case of primitive data type the values copy is passed as method arguments. In case of objects the copy of the object's reference itself is passed , so original reference and copy point to same object. So, in case you reassign the copy to some other object the original reference will still point to the original object .

Q: State different access modifier in java?

A: Public:- Public classes, member variables and methods are available or visible all other classes.

Private: - Private member, methods and inner classes as accessible to the class itself and its methods. They are not visible outside the class even to sub classes.

Protected: - Protected members, methods and inner classes are visible to all classes in the same package and in the subclasses even if they are not in same package .

Default: - when no access modifier is specified the members, methods and inner classes have default modifier they are visible or accessible to classes inside same package only.

Q. Is it necessary to import java.lang.* package? Why?

A:. No, The java.lang package is loaded by default by the JVM.

 

 



Comments
Search
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Monday, 26 May 2008 05:47 )