or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
:::section{.main}
In Java, the
abstract
keyword facilitates abstraction, which focuses on displaying only relevant details. Abstract classes and methods created using this keyword cannot instantiate objects directly and require inheritance for data extraction. Abstract methods, lacking implementation, are designed for overriding in subclasses. This approach simplifies complex systems by highlighting essential features.Syntax
:::
:::section{.main}
Characteristics of Abstract Keyword in Java
:::
:::section{.main}
Rules of Abstract Keyword
Before using the abstract keyword in Java in your code, you must know some rules. There are some important do's and do n'ts for using abstract keywords.
Do's :
Dont's :
abstract
keyword cannot be used when the' final' keyword is used.private
.static
.:::
:::section{.main}
Abstract Methods in Java
An abstract method is an undefined method, the type of method that does not contain any logic or definition but merely a declaration.
You cannot define the body of an abstract method at the time of its declaration. You must end all abstract methods with a semicolon whenever you declare them. You cannot declare abstract methods inside a regular (non-abstract) class; they are always present in abstract classes. Abstract methods serve as templates for abstract classes.
The syntax for the abstract method is given below :
Syntax:
:::
:::section{.main}
Abstract Classes in Java
Abstract classes are restricted classes whose objects cannot be created. You can only access an abstract class by inheriting it from another class. But you must have a question.
An abstract class is largely undefined. It consists of abstract methods which are not implemented at all. An abstract class actually serves the purpose of defining the blueprints of classes that are going to inherit from it.
An abstract class has a protected constructor (by default), which only allows derived types to initialize it.
Syntax:
In the above syntax, an abstract class is declared using the
abstract
keyword, and the method inside it is also an abstract method, which is declared using theabstract
keyword.Based on the ,above example, let's create a Java program to understand the abstract class.
:::
:::section{.main}
Examples of Abstract Keyword
Abstract class containing the constructor
Output:
Explanation:
alert
.display()
declared inside the abstract class.constructor
for the Student class, which calls thesuper
method with a string.display()
method is implemented in the child classStudent
, which prints the value of thealert
variable.AbstractEg
class, we have made an object of theStudent
class which extends the abstract classSchool
; we use that object to call thedisplay()
method printing Please pay the fees.Abstract class containing overloaded abstract methods
Output:
Explanation:
School
inside, for which we have two overloaded abstract methods nameddisplay()
.display()
, the method does not contain parameters, whereas the second one takes one string parameter.Student
class, which inherits from the abstract classSchool
.OverloadedAbstMethods
, we have created an object of theStudent
class, which first calls the abstract method, which prints Admission is made.display()
method, where we get both the strings as output.:::
:::section{.main}
Advantages of Abstract Keywords
:::
:::section{.main}
Abstract and Final Class in Java
Before going into the difference between
abstract
andfinal
, let's briefly discussfinal
classes.The term final class refers to a class that is declared using the Final keyword. Using the
final
keyword, you will finalize and close out the implementations of themethods
,variables
, andclasses
in this class.After declaring a class as a
final
class, inheriting from that class is impossible. Extending it to another class will give us acompile-time error
in Java. Also, we could not usefinal with abstract
as an abstract class needs to be inherited to implement its methods, whereas final restricts inheritance.Here is a small implementation of a final class:
If another class inherits from the final class, it will cause a
compile-time error
. The below code demonstrates this:The above code will give you the following error:
Output:
abstract
keyword is used to declare an abstract class.final
keyword is used to declare a final class.:::
:::section{.summary}
Conclusion
:::