Find method contents from a method in superclass? (Java/Eclipse) - java

I want to find the contents of the java.net.ServerSocket accept() method (because I am trying to override it so it returns a different type). Is this possible in Eclipse, or for that matter, in Java?
Thanks.

No, it isn't possible in Java. Because, you can not modify the return type of a function you override. The Java Tutorials Overriding and Hiding Methods says (in part)
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
I recommend you use the #Override annotation to protect yourself from acidentally overloading a method you intend to override.

Related

Which of the following are correct regarding an equals() method in java?

This is a question on one of my practice exams that has me stumped.
Potential Answers
An equals() method written in a class named C must have a parameter that is declared to be of type C, in order to override the equals() method in the Object class.
An equals() method must have a parameter that is declared to be of type Object, in order to override the equals() method in the Object class.
An equals() method written in a class named C must cast its parameter to Object, even if its parameter is declared to be of type C, otherwise it will not be able to properly compare the fields of current object to those of the parameter object.
A well-designed equals() method should check for and handle the case where its current object may be null
An equals() method in a subclass cannot refer directly to private fields in a superclass in order to compare them, so it should invoke the superclass equals() method to do this
The answer is #2 & #5. I don't understand why #1 is wrong and #2 is correct. Don't you have to compare one type C with another type C? And why is #5 correct. I don't understand this one at all. Can't subclasses access the private fields of a superclass?
I don't understand why #1 is wrong
Because:
Object::equals is defined as public boolean equals(Object), and
public boolean equals(SomeType) does not override public boolean equals(Object). This is actually an overload not an override. They are different operations.
Can't subclasses access the private fields of a superclass?
In general, no they can't. Check your lecture notes.
There are some exceptions that involve the subclass being declared inside the superclass.
Technically, answer #5 is only half right.
It is correct that the subclass cannot access private fields in the superclass, assuming we stay within the bounds of pure Java. The Java access rules forbid this. (But see above.)
It is correct that calling super.equals(other) could be a solution. And indeed, it usually is the correct solution.
It is incorrect to imply that calling super.equals(other) is the only solution.
Other possible solutions may include:
ignoring the superclass fields when testing for equality,
accessing the private fields of the superclass using public getters ... if they exist, or
accessing the private fields by abstraction-breaking reflection.
Of the alternatives, the first one is rarely applicable and the third is a really bad idea. But these alternatives mean that Answer #5 (as written) is technically incorrect.
If this is your practical exam: did your course material not cover this?
As for #1: the method signature is public boolean equals(Object obj), so that's what you have to override. Any other signature and it's not an override, it's "a completely different method".
As for #5: That's what private means. The fields are private to that class and only that class. If a subclass should have access to them, then they'd have to be protected, not private.
Consider the following equals method for C class.
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
C c= (C) o;
return id == c.id;
}
Explanation for 1 & 5 options:
If you declare the parameter of type C, then it will not be overriding.
Error:
Method does not override method from its superclass
Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
The method does override or implement a method declared in a supertype.
The method has a signature that is override-equivalent to that of any public method declared in Object.
superclass cannot access private fields of a class, so they need to call the superclass equals method to compare them.

Add general arguments in an abstract method (Java)

Say I have an abstract class called "Subsystem.java" and I have a class that extends it called "Drive.java". In Subsystem.java, I have a method called initialize with no arguments. How do I Override that method in Drive.java and add arguments? In other words, I want to be able to override a method with no arguments into one that has arguments. Is this even possible? Thanks.
No, that is not an override, but an overload. You still couldn't create an instance from Drive, since it would have an unimplemented abstract method.
Overrides have the exact same signatures.
Overloads have the same name, but different parameter types.
You can not override as like you describe.
When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
you can refer below link for more information about overriding a method in Java.
Method overriding in Java
or you can also refer Oracle Java documentation
Oracle Java document

Method Overriding of diffrent Return type to void

let's say we have this method
public class Animal {
public void eat() { }
}
and on another class that extends Animal has this method
public String eat(){}
is this considered as method overriding? because I've heard that you can use different return types in method overriding providing they have the same method arguments
This is neither overloading , nor overriding ! It is compilation error .
In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading.
Following are rules of method overriding in java which must be followed while overriding any method. private, static and final method can not be overridden.
1)Method signature must be same including return type, number of method parameters, type of parameters and order of parameters.
2)Overriding method can not throw higher Exception than original or overridden method. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception.
3)Overriding method can not reduce accessibility of overridden method , means if original or overridden method is public than overriding method can not make it protected.
You can only change the return type to a subclass of the original return type.
All answers are correct from a technical point of view because Void is a class in Java that no other class can extend and when methods are overridden the return-type has to be a sub-type of the original type (parentReturnType.isAsignableFrom(overriddenReturnType)).
But from a practical point of view the question is still valid and it would have made sense to make an excuse for the special case void. The only value a variable of the type Void can hold is null. Further no variable can have the primitive type void. Finally you can not compile this code:
Void result = object.notify();
So in the end it would have made sense to specify Java differently when 1.5 was introduced and allow overriding void return types with any other type.
Therefore the only reasonable answer is IMHO it is not possible because Java was designed such that this is not allowed.
This not correct Code, as two methods with same name should return same value, only arguments can be differents
Overriding is having same method with same arguments in the subclass
Overloading having same method name with different argumens
The return type of a method is not part of that method's signature.
When you call eat() on the subclass, java looks at the signature you provided (eat with no parameters) and starts looking for a method with this signature, starting in the class of the instance type of the object you call it on (and searches through its ancestors if it fails to find it).
So, methods called on a subclass will always run the overridden methods (they're found first).
A critical 'point' of OO (specifically polymorphism) is that a subclass can be passed in where the superclass was expected.
If a method wants to call the superclass' eat() method and get a void, but you passed a subclass into that method of which eat() returned a String; you can see that method will call the subclass' eat and get an unexpected value.
You can see why this could be a problem. :I
So, overriding methods must return the same type or a subclass of the type returned by the parent.
(Similar principles apply as to why you can return a subclass; I'll let you think about it c: )
It's neither overriding nor overloading, it creates compilation error.
Two methods with same name, different param numbers or different type of params known as overload method, return type may be same or different. It may be in same class or parent, child classes.
Two methods having same param numbers, same types, same return types but one is in parent class and other is in child class are known as overrided methods.

Clarification on overloading

When the parent class have an add method with 2 parameters, If we add new add method with 3 parameters in child class, shall we call this as over loading?
Thanks in advance.
Yes, since the method with two parameters is inherited by the subclass, the method with three parameters is said to be an overloading method.
class A
add(param1, param2)
class B
add(param1, param2) <-- inherited
add(param1, param2, param3) <-- overloading the above method
A quote from the official trail on Overriding and Hiding Methods:
Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods—they are new methods, unique to the subclass.
(As you probably already figured out, the method with three classes is not an overriding method.)
Yes, this is overloading. It would be overloading even if the method were in the same class as the method with two parameters.
Note that when there are different numbers of parameters (and no varargs parameters) overloading is reasonably simple. It gets a lot more complicated when you have methods with the same number of parameters - at that point the compiler has to choose the "best" method out of applicable candidate methods.
Also note that overloading is determined at compile time whereas which override is executed is determined at execution time based on the actual type of the object the method is called on.
Overloading is when methods have the SAME NAME but DIFFERENT SIGNATURE.
Overriding - when methods have IDENTICAL NAMES and IDENTICAL SIGNATURE.
Yes definitely a overloading and a nice feature of inheritance.
If the method name already exists but the parameters are different, then yes, this is overloading.
yep... it is overloading method even if the method is located in the same class.
ps: i assume the new method with three parameters has identical name and return type

can methods be overloaded based on access in java?

For example a class has public do, packaged do, protected do, and private do. If an instance of the class calls do it gets the private one, if a subclass calls it, then it gets protected, if a same package calls it, then it gets packaged and if anything else calls it, it gets public?
No.
It is a compile-time error to declare
two methods with override-equivalent
signatures (defined below) in a class.
Two methods have the same signature if
they have the same name and argument
types.
(From JLS §8.4.2)
class A
{
public Object do() { ... }
protected Object do() { ... }
Object do() { ... }
private Object do() { ... }
}
No. This will not compile with or without a subclass. Nor should it be expected too. How would the compiler have any idea which method to invoke? It's impossible.
To maybe make it a little clearer, a more distinctive overload--one that returns some other type than Object is not even an acceptable overload because the compiler would still have trouble determining which method to call. A weaker form of overloading would be even less acceptable.
No. This is an element that comprises its identity (just as you can't overload the name or return type of the method). You can only overload which variables are passed to the method.
If you tried to make a method public whose super method was private, you would get a compiler error, and your program would not run.
No, because this methods all have the same method signature:
Definition: Two of the components of a method declaration comprise the method
signature — the method's name and the parameter types.
Compiler can only distinguish between methods based on their signature.

Categories

Resources