0% found this document useful (0 votes)
32 views

Final - Abstract Keywords in Java

The final keyword in Java is used to declare variables, classes, or methods that cannot be modified. A final variable can only be initialized once, final methods cannot be overridden, and a final class cannot be extended. The abstract keyword is used to achieve abstraction and create abstract classes and methods, which may be partially implemented or not implemented at all.

Uploaded by

harshrajzala2312
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Final - Abstract Keywords in Java

The final keyword in Java is used to declare variables, classes, or methods that cannot be modified. A final variable can only be initialized once, final methods cannot be overridden, and a final class cannot be extended. The abstract keyword is used to achieve abstraction and create abstract classes and methods, which may be partially implemented or not implemented at all.

Uploaded by

harshrajzala2312
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Final keyword in Java:

In Java, the final keyword can be used while declaring an entity. Using the final keyword means that the value can't be modified in the
future. This entity can be - but is not limited to - a variable, a class or a method.

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is
called a "blank final" variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly,
a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases

Final Methods

Methods marked as final cannot be overridden. When we design a class and feel that a method shouldn’t be overridden, we can make this method final.
We can also find many final methods in Java core libraries.

Final Class

If the class is final, we can’t extend it to override the method and fix the problem. In other words, we lose extensibility, one of the
benefits of object-oriented programming.

https://www.youtube.com/watch?v=louYAgvTsLY (5 Mins video of Final Keyword, Final Method, Final


Class)

Abstract Keyword in Java


The abstract keyword is used to achieve abstraction in Java. It is a non-access modifier which is used to create abstract class and
method. ... However, it may also contain non-abstract methods. The method which is declared with abstract keyword and doesn't have any
implementation is known as an abstract method.

Abstract class in Java (https://www.youtube.com/watch?v=7nCQ8JPXZEo) 2 mins video

Abstract classes cannot be instantiated due to their partial implementation, but they can be inherited just like a normal class.

When an abstract class is inherited, the subclass usually provides implementations for all of the abstract methods in its parent class. However,
if it does not, then the subclass must also be declared abstract.
abstract class syntax
public abstract class DemoClass
{
//declare other methods and fields
}

Abstract methods in Java (https://www.youtube.com/watch?v=_MeavQdZU_g ) 5 mins video in


continuation of previous topic (Abstract Class)

Java abstract method


An abstract method is a method that is declared without an implementation i.e. without curly braces, and followed by a semicolon. If a class
includes abstract methods, then the class itself must be declared abstract.
Methods in an interface, that are not declared as default or static, are implicitly abstract so the abstract modifier is not used with interface
methods.

abstract method syntax


public abstract class DemoClass
{
//declare other methods and fields

//an abstract method


abstract void someMethod();
}

You might also like