NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Virtual function:
instance methods / non-static methods: virtual methods
class methods / static methods: non-virtual methods
abstract methods or a method that can be overridden are virtual methods. Any abstract, non-static or non-final methods in java is virtual function.

Note:
-> a class cannot have abstract methods, only abstract class can have abstract and non-abstract methods.
-> Cannot reduce the visibility of a method in subclass than a superclass.
If superclass is public then subclass can't be anything else than public. Subclass > visibility than superclass.

Interface:
An interface is a contractual blueprint that defines a set of abstract methods and constants.
Methods are default: Public and abstract
Constants: static and final.

An interface can declare but not define i.e. provide implementations of methods.
Subclasses implementing interface must provide implementation for methods.
A class implements interface but an interface extends interface. Because Subclasses implementing interface must provide implementations for methods, but interface cannot have implementation.

Private:
Interface can have private methods.
But private methods cant be accessed outside interface.
So interface can provide its implementation in interface.

Default:
Interface can have default methods.
It can be accessed outside interface.
Interface can provide its implementation in interface.

Static:
Interface can have static methods.
But it can't be overridden.
So interface can provide its implementation in interface.
It can be directly accessed using Interface for e.g. Interface.staticMethod(); Also using object.
If there is a definition of static method in subclass, then it is not Overriding, it is method hiding. (Also, for overriding @Override is used, but not for static methods' method hiding).

Interface cannot have final methods.

If static methods are overridden. It is not overriding, it is method hiding. Also no @Override is used.



If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass. This mechanism happens because the static method is resolved at the compile. Static method bind during the compile using the type of reference not a type of object.


class Complex {
public static void f1()
{
System.out.println(
"f1 method of the Complex class is executed.");
}
}

class Sample extends Complex {
public static void f1()
{
System.out.println(
"f1 of the Sample class is executed.");
}
}
public class Main {

public static void main(String args[])
{
Complex d1 = new Complex();

// d2 is reference variable of class Demo that
// points to object of class Sample
Complex d2 = new Sample();

// But here method will be call using type of
// reference
d1.f1();
d2.f1();
}
}


Output
f1 method of the Complex class is executed.
f1 method of the Complex class is executed.


Static methods are defined at the class level. In the case of the static methods, regardless of which object the reference is pointing to, it always calls the static method defined by the reference class. Here, the reference is of type One, so the static method of class One will be called.




     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.