Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've prepared a simple test app.</p> <pre><code>public class TEST { static abstract class Flight { String type = "Flight"; abstract String getDirection(); public String getType() { return this.type; } public void schedule() { System.out.printf("Scheduled a %s towards %s",this.getType(),this.getDirection()); System.out.println(); System.out.printf("ReadyToGo: %s",this.isReadyToGo()); } public boolean isReadyToGo() { return false; } } static class SouthWestFlight extends Flight { String type = "SouthWestFlight"; @Override String getDirection() { return "SouthWest"; } @Override public boolean isReadyToGo() { return true; } } public static void main(String... args) { new SouthWestFlight().schedule(); } } </code></pre> <p><strong>Output:</strong><br> Scheduled a Flight towards SouthWest<br> ReadyToGo: true</p> <p><strong>Conclusions</strong><br> Here the <code>SouthWestFlight</code> object is still a <code>Flight</code> Object.<br> But when you extend a class the child class overrides all the methods of parent class.<br> That is why <code>isReadyToGo()</code> of SouthWestFlight() returned true.<br> None of the properties are overriden.<br> That is why <code>this.getType()</code> returns <code>Flight</code>.<br> But if you override the <code>getType()</code> method with a new method in <code>SouthWestFlight</code> like this:</p> <pre><code>@Override public String getType() { return this.type; } </code></pre> <p>It will return <code>type</code> field of <code>SouthWestFlight</code>. Wondering why?<br> It is because, the first <code>getType()</code> method is defined in parent <code>class Flight</code>. So it returns <code>type</code> field of <code>class Flight</code>. The second <code>getType()</code> method is defined in <code>SouthWestFlight</code> so it returns <code>type</code> field of <code>SouthWestFlight</code>. </p> <p>Hope you've got it clear. Please comment if you found any mistake or got any doubt.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload