Posts

Showing posts with the label Java Methods

Scanner Class in Java: Complete Guide to User Input with Examples

Scanner Class. Scanner Class is present inside the java.util package. It is used for the take the input from user. Scanner Class method. nextByte();read byte data. nextShort();read short data. nextInt();read integer data. nextLong();read long data. nextFloat();read float data. nextDouble();read double data. nextBoolean();read boolean data. next();read string and character data. nextLine();read string data.

Method Overriding in Java: Definition, Rules, and Examples

Method Overriding Method Overriding is a process in Java where a subclass provides a specific implementation for a method that is already defined in its superclass. Key Points 1. Method Overriding requires inheritance. 2. To override a method, the subclass method must have the same name, return    type, and parameter list as the superclass method. 3. During method overriding: The subclass method definition is used (exposed). The superclass method definition is hidden. This concept is sometimes referred to as method hiding (though strictly         speaking, method hiding is for static methods). 4. Only instance methods (non-static methods) can be overridden. 5. Method overriding cannot be achieved within the same class; it only works between subclass and superclass. Example class Animal {     void sound() {         System.out.println("Animal makes a sound");     } } class Dog extends Animal {     @Overri...