OBJECT ORIENTED PROGRAMMING
Q.What is the difference between Constructor and Function or Method? Constructor will be automatically invoked when an object is created whereas method has to be called explicitly. Constructor needs to have the same name as that of the class whereas functions need not be the same. * There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. * There is no return statement in the body of the constructor. * The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.
Q. Define an abstract class
An abstract class is a class that is declared by using the abstract keyword. It may or may not have abstract methods. Abstract classes cannot be instantiated, but they can be extended into sub-classes. Points of abstract class :
1. Abstract class contains abstract methods.
2. Program can't instantiate an abstract class.
3. Abstract classes contain mixture of non-abstract and abstract methods. 4. If any class contains abstract methods then it must implements all the abstract methods of the abstract class. Simple class: A "simple" class is a class that an instance can be created from (via new operator)
Q. What is the difference between an instance variable and a class variableInstance variables have separate values for each instance of a class. Class variables maintain a single shared value for all instances of the class, even if no instance object of that class exists.You would use the static keyword to change an instance variable into a class variable.
Q. What is the difference between method overloading and overriding Method overloading deals with the notion of having two or more methods(functions) in the same class with the same name but different arguments. While Method overriding means having two methods with the same arguments, but different implementation. One of them would exist in the Parent class (Base Class) while another will be in the derived class(Child Class)
Q. what is the difference between private, protected and public or access specifiers. default: objects are visible from within their own class, or any other class in the same package. public: The public keyword is used in the declaration of a class, method, or field; public classes, methods, and fields can be accessed by the members of any class. protected: The protected keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class's subclasses or classes from the same package private: The private keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class.
Q. Define the purpose of type casting
Type casting means to explicitly convert one type to another.
For example, the following lines:
double x = 5.0;
int y = x;
will produce an error message, because of a possible loss of precision (any decimals will get lost when converting to int). The following will work however:
double x = 5.0;
int y = (int) x;
The programmer is forcing the Java compiler to accept the conversion; saying, in effect: "Please do this anyway, I know what I am doing".
Q.Difference between extends and implements
1)Extends keyword is used to implement the concept of inheritance in Java programming language while Implements keyword in Java programming language is used for implementing an interface by a class. 2)Extents is used when creating a subclass while implements is used when implementing an interface. 3)You can only extend one class in java, but you can implement...
Please join StudyMode to read the full document