java / devjava-content

55 stars 16 forks source link

inheritance missing links and extra suggestion - interface confusing parts - generic color issue #113

Closed Just-some-one closed 3 months ago

Just-some-one commented 3 months ago

for the static method on the override tutorial

for that code

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}

would not be better to add extra line

myAnimal.testClassMethod();

to explain that the static method that get called is depend on the variable type not the actual object type and giving a note that it bad practice to call static method using an instance but doing that just to show how things works

another point on the first section of that page to add link to the annotation tutorial on that paragraph

When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error. For more information on @Override, see the section Annotations.

https://dev.java/learn/annotations/

for https://dev.java/learn/inheritance/objects/#finalize

As of Java SE 9 the finalize() method had been deprecated. Overriding this method is now strongly discouraged. If you need to clean up some resources, you may do so by implementing the AutoCloseable interface. This point is covered in detail in the Java I/O section.

adding link to the I/O tutorial would be nice

for https://dev.java/learn/inheritance/abstract-classes/

Note: Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

a link to the interface tutorial would be nice

now for the interface tutorial

for the interface tutorial

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

also interface can have private instance method but only with body

and

The interface body can contain abstract methods, default methods, and static methods.

we could add private method to it also

for static method on interface

Starting with Java SE 9, you can define private methods in a interface to abstract a common piece of code from static methods of an interface while defining its implementation. These methods belong to the implementation, and they can be neither default and nor abstract when defined. For example, you could make getZoneId method private since it hosts a piece of code that is internal to the interface implementation.

it sound like that refactor piece of code from the static method will be in private method but it compile time error to call instance method from static context

for integrating default method part

The class PlayingCard implements the interface Card, and the class StandardDeck implements the interface Deck.

it missing the link to the playingCard and StandardDeck implementations which exist on the old tutorial site https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/PlayingCard.java

https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/StandardDeck.java

for generic

An "Out" Variable. An "out" variable holds data for use elsewhere. In the copy example, copy(src, dest), the dest argument accepts data, so it is the "out" parameter.

dest need to be wrapped by code tag to get the required color

sorry for long post and have a nice day :)

danthe1st commented 3 months ago

would not be better to add extra line myAnimal.testClassMethod(); to explain that the static method that get called is depend on the variable type not the actual object type and giving a note that it bad practice to call static method using an instance but doing that just to show how things works

I don't think it's a good idea to show bad practices unless there's significant benefit (even when explaining it's a bad practice). In my opinion, it's sufficient to show people how to use the good practices.

it missing the link to the playingCard and StandardDeck implementations which exist on the old tutorial site https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

I don't know about linking to off-site code for the tutorials. I guess it might be better to either include the necessary parts of the code inside the article, rewrite it in a way that makes it clear it would be a class to be created or just leave it as-is.

JosePaumard commented 3 months ago

Done, thank you for your reports!

Just-some-one commented 3 months ago

thanks for both of you :)