Introduction To Course PT5: @garima Sharma, Geu
Introduction To Course PT5: @garima Sharma, Geu
As no method body is present.Any concrete class(i.e. class without abstract keyword) that
extends an abstract class must overrides all the abstract methods of the class.
A few important rules
•Any class that contains one or more abstract methods must also be declared abstract
•The following are various illegal combinations of other modifiers for methods with respect
to abstract modifier :
• final
• abstract native
• abstract synchronized
• abstract static
• abstract private
• abstract strictfp
Final Keyword
The final keyword in java is used to restrict the user.
The java final keyword can be used in many context such as :
1. variable
2. method
3. class
Final Keyword usage
FINAL VARIABLE :
If you make any variable as final, you cannot change the value of final variable.
It will act like a constant.
FINAL METHOD:
If you make any method as final, you cannot override it.
FINAL CLASS:If you make any class as final, you cannot extend it.
Abstract and Final cannot be seen
together why??
In java, you will never see a class or method declared with both final and abstract keywords.
For classes, final is used to prevent inheritance whereas abstract classes depends upon their
child classes for complete implementation.
In cases of methods, final is used to prevent overriding whereas abstract methods needs to be
overridden in sub-classes.
Constructors in Java
• In Java, a constructor is a block of codes similar to the method.
• It is called when an instance of the class is created.
• At the time of calling constructor, memory for the object is allocated in the memory.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, ie. at least one constructor is called.
• It calls a default constructor if there is no constructor available in the class.
• In such case, Java compiler provides a default constructor by default.
Two types of Constructors
• No- argument Constructor(Default Constructor)
• Parameterized Constructor
Important Rules for Constructors
1. Constructor name must be the same as its class name.
2. A Constructor must have no explicit return type.
3. A Java constructor cannot be abstract, static, final, and synchronized.
4. We can use access modifiers while declaring a constructor.
5. It controls the object creation. In other words, we can have private, protected, public or
default constructor in Java.
Default Constructor Sample Program
Parametrized
Constructor
Sample
• A constructor which has a specific
number of parameters is called a
parameterized constructor.