It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is just a small one but i just need more clarity on these two.
By import you tell the compiler that my program is going to use imported classes so please make them available.
import java.util
By inheriting class you are going to use class properties and functions (which are being inherited) in child class.
class Maruti extends Car{
}
import allows you to use the imported class in the class you're currently writing.
Inheriting, or using the extends keyword allows you to implement the current class with the functionality of the class you are inheriting from.
For instance:
public class Animal
{
public void walk()
{
System.out.println("i am walking");
}
}
public class Cat extends Animal
{
public void meow()
{
System.out.println("Meow!");
}
public static void main(String[] args)
{
Cat catAnimal = new Cat();
cat.walk();
cat.meow();
}
}
So as you can see in the above example, because Cat extends Animal the Cat class can do everything the Animal class can.
import lets you see the class so you can inherit (aka extend) it.
Importing classes are simply allowing your class to have access to these classes without using their fully qualified names.
For example, if you have the following:
import javax.swing.JFrame;
public class Main{
//this class has ACCESS to the JFrame class, but it isn't a JFrame because it doesn't inherit (extend) from one
}
Your Main class would have access to the methods and variables in the class javax.swing.JFrame without having to call it with that full name, it allows you to simply use JFrame.
Inheriting a class is extending your own class to gain access to a classes methods and variables because your class "is a" inherited class.
Here is some code that doesn't "import" JFrame, it uses its fully qualified name to extend itself so that it has access to every method/variable inside the JFrame class (as long as the modifier is public or protected).
public class MyFrame extends javax.swing.JFrame {
//this class IS a JFrame, and is not importing one, it's instead using the fully qualified name to extend itself and make it a JFrame.
}
Related
This question already has answers here:
Multiple Inheritance and class Object
(5 answers)
Closed 9 years ago.
Well Java does not support multiple inheritance in java..
But wait in eclipse we can see any class extends OBJECT class by default, we can see all methods of Object class if we try to add unimplemented methods.
Now MY POINT IS i can make my class extend any class for example Thread.
So now my class extends Thread by user defined side and Object by default...
that means multiple class inheritance ?
Behavior similar to multiple inheritance can be seen with Java interfaces:
// implements BOTH Runnable AND ActionListener
public class MultipleInterfaces implements Runnable, ActionListener {
#Override public void run() {}
#Override public void actionPerformed(ActionEvent e) {}
}
Multiple inheritance would be like this:
// Not allowed, complete nuts
public class Amalgam extends ArrayList<Thread>, JPanel, Font {
public Amalgam() {
super(); // <- and what would this do?
}
}
A class can only have one superclass i.e. in java one class can only extend one class. If one is not specified, then it is implicitly extends to Object.
So suppose the class is MyClass and it extends MySuperClass. As MyClass extends MySuperClass so it will not extend directly Object. But MySuperClass class itself is not explicitly extending any class so it extends Object and in turn MyClass also extends Object in the hierarchy.
So it is not Multiple inheritance rather it is Multilevel inheritance. Hope it helps.
The behavior mentioned by you is multi-level inheritance which java supports by default
This is MultiLevel inheritance and not Multiple inheritance.
any class extends OBJECT class by default
That means Thead class extends Object too.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am confused on one scenario of interface.Below is the code in which I haven't declare the toString() method in interface.
It is the method of object class.But still toString() method is able to execute from the parent class reference variable .But the rule says that before calling the child class method it first look method in interface, if method is present then call the child class method but in this scenario How toString() is executed without declaration in interface please explain me
public interface Parent {
void show();
}
class Base implements Parent {
public void show() {
System.out.println("hey it is going to be execute");
}
public String toString() {
return "itspossible";
}
public static void main(String[] args) {
Parent parent = new Base();
System.out.println(parent.toString());
}
}
This works because every class extends the Object class implicitly. Therefore any implementation of any interface, has the toString() method available.
Its because the toString method is included on Object which every class is derived from. The method is being called from the Object class.
I found this question answered by Jon Skeet that explains it well:
Does an interface by default extend Object?
In the end, parent is an object reference from Base class. Note that all classes extends from Object class, thus having the methods defined in Object class such as toString.
it is because Object class is a base class for every class and it includes toString method. So when you call toString with parent's reference, it'll call parent's toString().
The variable parent is just a reference which is referring to its subtype Child. Here you are creating an object of Child which by default extends Object class. You are overriding the toString method hence giving this output.
You may want to have a look at the following discussion. The Java language works as if there was a super interface that all interfaces, including the one above in your comment, and the Object class inherit from, which declares all the methods that the Object class implements. Unfortunately, that interface doesn't really exist, the compiler just behaves as if it would exist.
You are not calling toString method of interface but it's Bash Class method. Each object have a default implementation of toString method even you not explicit implement it.
How can instance of interface access method of Object class?
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
There are many places in Java where a class can be defined. It can be public, static, anonymous, etc.
I know there are a bunch of them. Does someone have a complete list of all the types of class that one can come across in Java?
You will want to read the JLS Chapter 8: Classes:
ClassModifiers:
ClassModifier
ClassModifiers ClassModifier
ClassModifier: one of
Annotation public protected private
abstract static final strictfp
But you will also want to note that
The access modifier public (§6.6) pertains only to top level classes (§7.6) and to member classes (§8.5), not to local classes (§14.3) or anonymous classes (§15.9.5).
The access modifiers protected and private (§6.6) pertain only to member classes within a directly enclosing class or enum declaration (§8.5).
The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.
If you need more detail, please read section 8.1, it deals with the class declaration (which you are likely interested in).
Outer i.e. non - nested classes can be applied with public and
default modifiers
Nested classes can be static
Inner classes can be private, protected, default and public.
All classes can be marked as abstract or final
An abstract class can never be final and final class can never be abstract
public class ClassModifier {
/* a private class */
private class PrivateInnerClass {
}
/* static nested class */
public static class StaticNestedClass {
}
public void methodForInnerClass() {
/* A class declared inside a method */
class MethodLocalInnerClass {
}
}
}
Well in a much simpler way, you can simply open eclipse IDE, create a new class and play around with all the available options and create some classes.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
class demo {
interface test {
}
}
is it possible in Java innerclasses?
I'm not sure if I exactly understand your question, but I think the answer is: Yes, a class can declare an inner interface.
Yes, inner classes are possible in Java.
In your code it looks like you're talking about interfaces though. Having inner interfaces are possible, too:
public class Demo {
public static interface Inner {
}
}
You can declare inner classes, too:
public class Demo {
private class Inner {
}
}
Syntactically interface inside a class is correct .
But it depends upon your usage. Logically it will be correct depending upon your usage.
You can have any enum, class, interface or annotation inside one another.
public interface A {
public #interface B {
public class C {
public enum D {;
public interface E {
// keep on going.
}
}
}
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Right now I am confused with inheritance and object of class. We can do the same using object as inheritance does.
ok here is first code
public class MainForm {
GUI g = new GUI();
g.show();
g.destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
now second one
public class MainForm extends GUI {
//GUI g = new GUI();
void show();
void destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
both code do the same.Right? then
Why should I extend a class when i can do the same using an object of class?
Inheritance is often used when you want to create multiple classes of a parent class, without duplicating a lot of logic. I may have a class named Animal which has methods speak(), walk(), and sleep(). I also may want to have specific instances of Animal like Cat and Dog.
Rather than implementing all three of those methods individually, I can write them in Animal and then have my other classes extend Animal to make use of those, as well as add any methods specific to those classes (like claw() or fetch()).
The benefits are reduced code duplication and better 'abstraction' of the objects. However, there are some drawbacks as well, as this article points out. It is important to know when inheritance is useful, and when it is not.