Difference Betwixt Extends In Addition To Implements Keywords Inward Java

Difference Betwixt Extends In Addition To Implements Keywords Inward Java - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul Difference Betwixt Extends In Addition To Implements Keywords Inward Java, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel core java interview question, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Difference Betwixt Extends In Addition To Implements Keywords Inward Java
link : Difference Betwixt Extends In Addition To Implements Keywords Inward Java

Baca juga


Difference Betwixt Extends In Addition To Implements Keywords Inward Java

Though both extends in addition to implements keyword inwards Java is used to implement Inheritance concept of Object-Oriented programming, at that spot is a subtle departure betwixt them. The extends keyword is mainly used to extend a cast i.e. to practise a subclass inwards Java, spell implements keyword is used to implement an interface inwards Java. The extends keyword tin too hold out used past times an interface for extending only about other interface. In social club to ameliorate empathize the difference betwixt extends in addition to implements, you lot too demand to larn in addition to empathize the departure betwixt cast in addition to interface inwards Java. Though both are an integral purpose of application evolution using object oriented methodology, an interface is to a greater extent than abstract than cast so it is used to define API or contract.

On the other hand, a cast provides the concrete implementation of interface i.e. the actual code which does the job. Though a cast tin too hold out abstract to define a contract, it's the chore best done past times an interface inwards Java. Even Joshua Bloch has advised in Effective Java nearly using interfaces to declare Type inwards Java, every bit it promotes multiple inheritances inwards Java, which is restricted every bit compared to C++.

Even after Java 8, solely multiple inheritances of type in addition to behaviour is supported inwards Java (by introducing default methods inwards Java 8), but multiple inheritances of the solid soil is all the same non supported. If you lot desire to larn to a greater extent than nearly why the multiple inheritance is non supported inwards Java, run across here.




What is pregnant of extending a class

Influenza A virus subtype H5N1 cast inwards Java tin extend only about other cast to acquire out a subclass. When cast B extends the cast A, B becomes subclass (child) in addition to Influenza A virus subtype H5N1 becomes superclass (parent). Influenza A virus subtype H5N1 subclass tin reuse all the features of the raise cast in addition to code reuse is a major argue for extending a cast but few understands that to a greater extent than of import is the defining human relationship which is after leveraged past times Polymorphism.

For example, if cast B extends cast Influenza A virus subtype H5N1 in addition to so a reference variable of type Influenza A virus subtype H5N1 tin gibe object of B, which agency Influenza A virus subtype H5N1 forthwith acquire out a polymorphic because it tin gibe both Influenza A virus subtype H5N1 in addition to B. This gives nativity to a technique of creating flexible software, known every bit programming for interface than implementation where you lot e'er exercise variables of raise cast or interface to define centre algorithm. The practise goodness of doing this is whatsoever novel cast which extends raise cast volition convey access to that algorithm.



Here is an illustration of extending a cast inwards Java:

class A {   public void show() {     System.out.println("show");   } }  class B extends A {   public void display() {     System.out.println("display");   }    public void show() {     System.out.println("better show");   } }  public class Main {    public static void main(String[] args) {      A a = new B(); // possible because B extends A     a.show(); // this volition forthwith telephone band to show() method of cast B   } }  Output ameliorate show

You tin run across that past times extending cast A, B forthwith convey access to show() method, it tin fifty-fifty override the behaviour of show() method, which is known every bit method overriding inwards Java. This immense ability comes past times using the extends keyword.

You tin too read the Head First Design Pattern inwards Java to larn to a greater extent than nearly the technique of programming for interface than implementation. The majority is too late updated to encompass Java SE 8.

 at that spot is a subtle departure betwixt them Difference betwixt extends in addition to implements keywords inwards Java



What is pregnant of implementing an interface

The pregnant of implementing an interface is non real unlike than extending a cast but it comes alongside an additional caveat. When a cast implements an interface it has to supply an implementation of all methods declared within interface. If It doesn't want to supply all implementation, it tin declare itself every bit an abstract class. Not next this rules agency your programme cannot compile. There are many examples of implementing an interface inwards Java code, but I believe implementing Runnable is the most popular. This is used to practise a Runnable project which tin hold out executed past times a thread.

class R implements Runnable{ public void run(){    System.out.println("do nothing");   } }

An event of R forthwith tin hold out passed to whatsoever method which expects a Runnable e.g. you lot tin piece of work past times this to both execute() in addition to submit() method of ExecutorService cast to run that project inwards a thread pool. Since our cast has implemented run() method, the unmarried method from interface Runnable, it is a concrete class. If it had non implemented the run() method you lot convey to acquire out far abstract past times adding abstract keyword inwards front end of cast e.g.

abstract class R implements Runnable{  }

 at that spot is a subtle departure betwixt them Difference betwixt extends in addition to implements keywords inwards Java


Important points

Here are only about of the of import points related to extends in addition to implements keyword inwards Java:

1) Influenza A virus subtype H5N1 cast tin solely extend only about other cast inwards Java, exercise extends keyword alongside the interface volition number inwards compilation fault every bit shown below:

interface I{  }  class A{ }  class B extends A{  }  class C extends I{  }

When you lot compile this root file you lot volition acquire the next error:

$ javac Main.java Main.java:27: no interface expected hither class C extends I{ ^ 1 error


2) Influenza A virus subtype H5N1 cast tin solely extend i class, trying to extend to a greater extent than than cast volition i time again number inwards compilation fault every bit shown below:

class C extends A, B{  }  $ javac Main.java Main.java:27: '{' expected class C extends A, B{ ^ 1 error


3) An interface tin exercise extends keyword to practise subinterfaces inwards Java e.g. next is perfectly legal inwards Java

interface J extends I{  }


4) An interface tin fifty-fifty extend multiple interfaces past times using extends keyword every bit shown below:

interface k extends I, J{  }


5) Influenza A virus subtype H5N1 cast tin exercise implements keyword to implement i or to a greater extent than interfaces inwards Java every bit shown below:

class D implements I, J, K{  }


6) Influenza A virus subtype H5N1 cast tin too exercise both extends in addition to implements keyword together to extend a class and implement an interface, it's quite mutual inwards Java every bit shown inwards the next example:

class E extends A implements I, J{  }

When a cast does that it is forthwith a subclass of Influenza A virus subtype H5N1 in addition to subinterface of I in addition to J, which agency you lot tin exercise a reference variable of cast E in addition to interface I, J to shop cast E object every bit shown below:

I i = new E(); J j = new E(); Influenza A virus subtype H5N1 a = new E();

This gives immense ability to cast E inwards price of using Inheritance in addition to Polymorphism.


7) Last but non the least, an interface cannot exercise implements keyword to implement an interface, it's illegal inwards Java in addition to compiler volition throw an fault every bit shown below:

interface L implements J{  }  javac Main.java Main.java:49: '{' expected interface L implements J{ ^ 1 error


That's all nearly the difference betwixt extends in addition to implements keyword inwards Java. It's clear that extends is to a greater extent than frequently than non used to extend a cast in addition to implements is to a greater extent than frequently than non exercise to implement an interface. Though at that spot are to a greater extent than subtleties e.g. an interface tin too extend only about other interface to acquire out a subinterface, a cast tin too hold out abstract fifty-fifty after implementing an interface but what doesn't alter is that they both are used to practise the parent-child human relationship inwards Java.

If cast B extends only about other cast Influenza A virus subtype H5N1 it becomes its children in addition to cast Influenza A virus subtype H5N1 becomes a parent. Same is truthful alongside the interface. You tin farther read Head First Java in addition to Head First Object Oriented Analysis to larn to a greater extent than nearly basic edifice blocks of Object-Oriented programming inwards java.


Other Java Interview Questions you lot may like
  • The departure betwixt Aggregation, Association, in addition to Composition inwards Java? (answer)
  • The departure betwixt Abstract cast in addition to Interface inwards Java? (answer)
  • The departure betwixt Inheritance in addition to Polymorphism inwards Java? (answer)
  • The departure betwixt Abstraction in addition to Polymorphism inwards Java? (answer)
  • Why Composition is ameliorate than Inheritance inwards Java? (article)
  • The departure betwixt static in addition to non-static variable inwards Java? (answer)
  • Why abstract cast is of import inwards Java? (opinion)

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Thanks for reading this article, if you lot convey whatsoever incertitude or question, postal service a comment in addition to if you lot similar the article, don't forget to portion alongside your friends. 


Demikianlah Artikel Difference Betwixt Extends In Addition To Implements Keywords Inward Java

Sekianlah artikel Difference Betwixt Extends In Addition To Implements Keywords Inward Java kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Difference Betwixt Extends In Addition To Implements Keywords Inward Java dengan alamat link https://bestlearningjava.blogspot.com/2017/05/difference-betwixt-extends-in-addition.html

Belum ada Komentar untuk "Difference Betwixt Extends In Addition To Implements Keywords Inward Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel