oops basic questions Part-3

What is final?

A final is a keyword in java. If final keyword is applied to a variable, then the variable will become a constant. If it applied to method, sub classes cannot override the method. If final keyword is applied to a class we cannot extend from that class.

What is a method?
Methods are used to perform some task.
Methods manipulate the data.
Methods may change the sate of an Object.
Methods returns a values at least void.
Types of Methods
instance methods
static methods
abstract methods
final methods
native methods
synchronized methods
What is an abstract method?
An abstract method is a method that doesn’t have a body. It is declared with modifier abstract.
we need to provide implementation for abstract methods in its subclasses
What is a native method?
A native method is a method that is implemented in a language other than Java.
What are the restrictions placed on static method?
We cannot override static methods.
We cannot access any object variables inside static method.
this reference also not available in static methods.
What is static initializer block? What is its use?
A static initializer block is a block of code that declares with the static keyword.
It normally contains the block of code that must execute at the time of class loading.
The static initializer block will execute only once at the time of loading the class only.
How parameters are passed to methods in java program?
All java method parameters in java are passed by value only. Obviously primitives are passed by value. In case of objects a copy of the reference is passed and so all the changes made in the method will persist.
How can we call a method or variable of the super class from child class?
We can use super.method() or super.variable syntax for this purpose.
What is an abstract class?
An abstract class is an incomplete class. An abstract class is defined with the keyword abstract. We cannot create an object of the abstract class because it is not complete. It sets a behavioural protocol for all its child classes.
How will you define an abstract class?
An abstract class is defined with the keyword abstract E.g.:
public abstract class MyClass { }
What is an interface?
An interface is a collection of method declarations and constants. In java interfaces are used to achieve multiple inheritances. It sets a behavioural protocol to all implementing classes.
An Interface is 100% abstract class. All the methods in interface are abstract only.
How will you define an interface?
An interface is defined with the keyword interface. Eg:
public interface MyInterface { }
What must a class do to implement an interface?
It must identify the interface in its implements clause. Also it must provide definition for all the methods in the interface otherwise it must be declared abstract.
What are the differences between an abstract class and an interface?
An abstract class can have concrete methods, which is not allowed in an interface. An interface is 100% abstract class
Abstract class can have private or protected methods and variables --only public methods and variables are allowed in interface.
Abstract class can contain instance variables but the variables in the interface must be final and static and public
We can implement more than one interface, but we can extend only one abstract class.
Interfaces provides loose coupling where as abstract class provides tight coupling.
An abstract class contains Constructor where as interface can not
What is a Constructor?
Objects are constructed.
We cannot make a new Object without invoking the Constructor.
And even more we cannot create a new Object without invoking the Constructors of its super classes
Every class even abstract class has at least one constructor.
The compiler will create a default constructor if we do not provide the constructor
Constructors are used to initialize an Object.
The name of the Constructors must be the name of class.
Constructors do not have a return type
Constructors are called implicitly when we create an Object.
Constructors cannot be called explicitly.
What is the difference between a constructor and a method?
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. We cannot invoke a constructor directly. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. (Means without that the method does nothing)