What are class declaring rules?
What is constructor?
Constructor is special kind of method which initialize the instance of class.Constructor don't have any return type even void also.When class is instatiated constructor is called first and it initialize the instance variables of class.If we don't provide any constructor jvm provides the default no argumant constructor.The first call in constructor is this() call to same class constructor or super() call to super class constructor.If we add parameterized constructor then jvm don't provide the default constructor
Can Static methods be overriden?
No Static method cann't be ovveriden
Can Super Class Method be called from Overriden Method in its Subclass ?
Yes Using super Keyword in subclass method we can call the super class verison
What is super and this
- Every Java File has single Public class declared in it
- Java File can have multiple non public classes declared in it
- If there is a Public class declared in Java File then name of java file must be same as Public class name
- If there are only non public classe in java file then name of Java file can be anything
- Java File can have multiple non public classes containing main method in each one of them
- At the top of Java File there is a package statement then thre are import statements and then the class declartaion
What is constructor?
Constructor is special kind of method which initialize the instance of class.Constructor don't have any return type even void also.When class is instatiated constructor is called first and it initialize the instance variables of class.If we don't provide any constructor jvm provides the default no argumant constructor.The first call in constructor is this() call to same class constructor or super() call to super class constructor.If we add parameterized constructor then jvm don't provide the default constructor
Differences between method overloading and method overriding?
Overloading
|
Overriding
| |
Arguments or Parameters
|
Must change
|
Number and Type of parameters must be same
|
Return type
|
Can change
|
No except Covariant return types which are available from Java 1.5 version
|
Access
|
Can change
|
Accessibility cannot be narrowed but it can be broaden.
|
Exceptions
|
Can change
|
Can throw all,none or only subset of exception thrown by overridden method.Can’t throw broader or new checked exceptions
|
Method Invocation
|
Method invocation happens at compile time.
|
Method invocation happens at runtime time.
|
If subclass contains extra methods which are
not there in super class tehn can we invoke these methods on superclass reference?
No. At compile time we will get error
e.g.
Class A
{
Public void getX(){}
}
Class B
{
Public void getX(){}
Public void getY(){}
}
A a=new B();
a.getX(); //Compiles
a.getY();//Compilation fails
Differentiate between Interface and Abstract class?
Abstract Class
|
Interfaces
|
Abstract class can have both abstract and non abstarct methods
|
Interface have only abstract methods
|
If class contains one or more abstract methods that class must be declared as abstract
|
Interface is by default abstract.All methods are in interfaces are abstract by default
|
Class can extend single abstract class
|
Class can implement multiple interfaces
|
Abstract class can be public ,private ,protected
|
Interface can only be Public
|
Abstract class can contain instance variables
|
Interface contain only static variable
|
Abstract class has a constructor as it falls in Object class hierarchy
|
Interface don’t have constructor
|
Abstract classes are faster
|
Interfaces are slow because there is extra level of indirection in calling the actual class’s method
|
Can Interface implement any other interface or extend any other Interfaces?
Interface cannot implement any other interface but Interface can extend any number of other interfaces
Can a class be decalred as abstarct withot any abstarct methods?
Yes. Abstract class can have zero or more abstract methods
Can abstract class contains main() method?
Yes as main() method is static it don't require the instance of class to invoke it
Can we declare interface methods as private?
No :Implicitly methods of interface are public and member variables are implicitly public static
Can we declare interface methods as static?
No. Interface contains only non static abstarct methods
No. Interface contains only non static abstarct methods
What if main method is declared as private?
Program compiles fine but at run time ‘Main method not public’ exception will be thrown
What if static is removed from main method?
Program compiles and at run time throws error ‘NoSuchMethodError’
What if we declare public void static main()?
Nothing order doesn’t matter. Program compiles and runs properly
What happens if we don’t pass string array as argument to main method?
Program compiles and at run time throws error ‘NoSuchMethodError’
If we don’t provide the argument to main method at runtime then the String array will be empty or null ?
String array will be empty but never null we can test by printing args.length which prints ‘0’
Can there be multiple nonpublic calluses in java file each containing main method
Yes
Can abstract class contain main method ?
Yes
What is final?
Final means constant
We can use Final in below three places
1) Final Classes: Can’t be sub classed
2) Final methods: Can’t be overridden
3)Final variables : value once assigned while declaring final variable cannot be changed
If object reference marked as final can we change the state of final object
Yes Object reference is marked as final means we cannot assign new object to that object reference.
But we can change the internals (state) of object whose reference is marked as final
Analogy :We can change the internals of our home but not the address of our home
No comments:
Post a Comment