Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Use Single Inheritance in Java
WhatsApp
Kiran Thakur
5y
19.4k
0
0
100
Article
Introduction
Now, we will first write a program using a "has a relationship " in Single Inheritance.
Step 1
Let's open Notepad and enter the following code.
Code
class
Test
{
void
show()
{
System.out.println(
"welcome in java method"
);
}
}
class
demo
{
public
static
void
main(String arg[])
{
Test t =
new
Test();
t.show();
}
}
Step 2
Name it "hasrel.java" and save the file in any location. I saved at "
C:/kiran/program"
.
Step 3
Open a command prompt (press window+R and enter cmd and hit OK).
Step 4
Go to the "command prompt".
Step 5
Go to
"C:/kiran/program"
by cmd.
Step 6
Now, enter the following code for cheking my Java file is compiled or not.
javac hasrel.java
My Java file is compiled successfully.
Step 7
Enter the following code into the command prompt. Press Enter and see the output.
Java demo
//demo is a class name that is written in my "hasrel.java" file
Output
We will now enter the program using a "is a relationship " in Single Inheritance.
Step 8
Enter the following code in Notepad.
Code
class
Test
{
void
show()
{
System.out.println(
"welcome in java method!!!"
);
}
}
class
demo
extends
Test
{
public
static
void
main(String arg[])
{
demo d =
new
demo();
d.show();
}
}
Step 9
Now, enter the following code for cheking my Java file is compiled or not.
javac hasrel.java
My Java file is compiled succesfully.
Step 10
Repeat Step 7.
Output
I hope you like this Java file.
Happy Codding.
How we are use inheritance in java
Inheritance
Inheritance in Java
Up Next
Ebook Download
View all
Solutions Manual to Objects First with Java – A Practical Introduction using BlueJ
Read by 1.8k people
Download Now!
Learn
View all
Membership not found