Part 3 Flashcards
How the declaration of abstract method looks like? What do you know about implementation of abstract method?
Abstract method has only declaration and it doesn’t have implementation. (note it is declared without curly braces {} )
public abstract int getNoWheels();
Child classes are responsible to provide implementation.
abstract class Wheel { public abstract void methodOne(); public abstract void methodTwo(); }
What if a class extends any abstract class?
If a class extends any abstract class then compulsory we should provide implementation for every abstract method of the parent class otherwise we have to declare child class as abstract. If we declare child as abstract then the code compiles fine but child of child is responsible to provide implementation for all abstract methods.
Which modifiers can’t be used together with abstract modifier and why?
final, static, syncronised, native, strictfp, private
What is the difference between final and abstract with respect to methods and classes?
For abstract methods we should override it in the child class to provide implementation. Whereas for final methods we can't override it hence abstract final combination is illegal for methods. For abstract classes we should create child class to provide implementation whereas for final class we can't create child class. Hence final abstract combination is illegal for classes. Final class cannot contain abstract methods whereas abstract class can contain final methods.
From where the member is visible if it is declared as public. What we need to pay attention when we are trying to access the member from another class, package?
Before checking whether member is accessible outside of package, be sure that class is also accessible outside of package. Even if member is declared as public but class is visible only from the same package, you will take compile time error when you will try to use that member inside anothor package.
What if memeber is declared as private? What kind of methods can’t be marked with private?
It is accessible only within the same class.
Private methods are not visible in child classes where abstract methods should be visible in child classes to provide implementation, hence private - abstract combination is illegal for methods.
From where is member visible if it is declared as protected?
Within current package or in child classes from outside
We can access protected members within the current package anywhere either by child reference or by parent reference.
But from outside package we can access protected members only in child classes and should be by child reference only that is we can’t use parent reference to call protected members from outside package.
package pack1; public class A { protected void methodOne() { System.out.println("methodOne is executed"); } }
package pack2; class B extends A { public static void main(String[] args) { A a = new A(); a.methodOne(); // compile error because protected method can't be // used in parent reference in child in outside package B b = new B(); b.methodOne(); // this is ok A a1 = new B(); a1.methodOne(); // compile time error due to usage of protected member in parent type reference } }
Which access modifiers we can use with local variable (declared inside the method)?
For local variable (declared within method) only the final modifier is available - you can’t use public, private, protected modifiers on local variable, it will give you error in such case.
When you declare local variable with final, you can’t change it then (like constant).
What kind of objects in java could be declared with strictfp modifier?
strictfp is the modifier applicable for methods and classes but not for variables
What is the strictfp modifier use for?
usually the result of floating point of arithmetic is varing from platform to platform, to overcome this porblem we should use stritfp modifier
if a method declare as the strictfp then all the floating point calculations in that method has follow IEEE754 standard, so that we will get platform independent results
What is it mean when class is declared with strictfp modifier?
If a class is declared as strictfp then every concrete method(which has body) of that class has to follow IEEE754 standard for floating point arithmetic.
What is the difference between abstract and strictfp? When we can use the abstract strictfp combination and when not?
strictfp method talks about implementation where as abstract method never talks about implementation hence abstract, strictfp combination is illegal for the methods. But we can declare a class with abstract and strictfp modifiers simultaneously. That abstract strictfp combination is legal for classes but illegal for methods
Can we declare a formal parameters of methods as final?
The formal parameters of a method simply acts as local variables of that method, hence it is possible to declare formal parameters as final. If we declare formal parameters as final then we can’t change its value within the method.
What kind of objects can be declared with static modifier?
static is the modifier applicable for methods, variables and blocks. We can’t declare a class with static but inner classes can be declaring as the static
For instance and static variables JVM will provide default values but if instance and static declared as final JVM won’t provide default value compulsory we should perform initialization whether we are using or not . For the local variables JVM won’t provide any default values we have to perform explicitly before using that variables , this rule is same whether local variable final or not.
Which variables can be declared as static and what is it mean?
In the case of instance variables for every object a separate copy will be created but in the case of static variables a single copy will be created at class level and shared by all objects of that class. Instance variables can be accessed only from instance area directly and we can't access from static area directly. But static variables can be accessed from both instance and static areas directly.