+ 2

What is the difference between importing and inheriting in Java?...

Can we do the same stuffs with extends, that we do while using import... For eg. can we use methods of Scanner class while using extends and inheriting the class?...

15th Apr 2019, 4:17 PM
Paranoid
Paranoid - avatar
3 Antworten
+ 4
Import means that you can use a class without using the full name/source of it. For example: java.util.Scanner scan = new java.util.Scanner(System.in); If you have a lot of java classes it would get really annoying and I think it would not be funny to read your code. To use import is like a shortcut. It makes your code readable and you don't have to write so much. Inheritance is not the same. So if you want to extend a class from the java api than you need to import this class and than you can extend it. I never tried it with the Scanner class. ;) But it would looks like this: import java.util.Scanner; class MyScanner extends Scanner{ } Or without import: class MyScanner extends java.util.Scanner{ }
15th Apr 2019, 6:40 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
Importing includes the knowledge of and functionality of a class or library in your current code. Inheriting includes the properties and functionality of a parent class in a child class.
15th Apr 2019, 11:46 PM
Sonic
Sonic - avatar
+ 3
I found this answer: "You must import subclass too if it is in another .java file as parental class". Notice, java.lang is imported as default. If you import java.util.Scanner, you can still call Scanner.getClass(). getClass is inherited method of class java.lang.Object.
15th Apr 2019, 6:55 PM
zemiak