Yes...(but not directly)
We can create the object to An Interface / Abstract class.
As per the java specification we are not allowed to create object to interface and abstract class directly. But we can do that indirectly with the help of anonymous inner type.
Interface:
An interface is one which contains only abstract methods( methods don't have body).
Any variable declared inside an interface are by default public static final and should be initialised
Syntax:
Abstract Class:
An Abstract class is a class that can contain abstract methods or concrete methods( methods those have body) or both.
Syntax:
What happens when we try to create object:
We have an interface SampleInt which contains an abstract method.
SampleAbs is the abstract class with a concrete method showSampleAbs().
Now we are trying to create object to the above defined interface and abstract class in the class App
So , as the compiler is not allowing us to create the object to interface and abstract class we can say We can't create object to an interface or abstract class directly .
How to create object to interface and abstract class:
We can create the object to interface or abstract class indirectly by using anonymous inner type.
App class shows creating object to SampleInt and SampleAbs.
By providing anonymous inner class to Abstract class or interface we can say they have intantiated indirectly.
We can call showSampleAbs() by using SampleAbs object sampleAbs aswellas we can call sampleIntMethod() defined in interface by using it's instance sampleInt.
Calling methods defined inside anonymous type:
Suppose we have defined some more methods inside the implementation (anonymous class), then we can't call them by using instance since the methods not available at the time of object creation.
Calling the methods that are defined inside anonymous inner type will be as shown.
Note:
Anonymous inner type is simply providing implementation to the corresponding abstract class / interface.
So we are able to create object to abstract class and an interface by providing implementation in the form of anonymous inner type.
You can observe the behaviour by practising this code.
public interface Interface {
ReplyDeletepublic void m();
}
public class ImplementedClass implements Interface{
public void m() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
Interface i = new ImplementedClass();
i.m();
i.toString();
i.hashCode();
Interface ii = new Interface()
{
public void m()
{
}
public void m1()
{
}
}.m1();
System.out.println(i);
System.out.println(ii);
}
}
I am unable to call like AnnonymusClss(){public void newmethod(){}}.newmethod();
Hi Sekhar,
DeleteAs i described in the article, you should call a method available as part of anonymous class with the help of the object directly(if it is already part of that interface), we should not use reference there as the method return type is void and you are equating it to Interface reference which is declined.
here we go,
public class ImplementedClass implements Interface{
public void m() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
Interface i = new ImplementedClass();
i.m();
i.toString();
i.hashCode();
//should not use reference as method m1() is newly //defined in your anonymous class and is not there in the actual interface.
new Interface()
{
public void m()
{
}
public void m1()
{
}
}.m1();
System.out.println(i);
System.out.println(ii);
}
}
If m1() is also available as part of your Interface then you can approach in your previous way
like
Interface ii = new Interface()
{
public void m()
{
}
public void m1()
{
}
}.m1();
I think u have understood the reason , if not feel free to revert back.
And a suggestion, Avoid usage of keywords as your class/interface names. You should follow the naming conventions given by Sun ..
Nice blog.Screenshots helped a lot. ..thanks
ReplyDeletehi lavanya ..you are welcome
DeleteThis comment has been removed by the author.
ReplyDeleteHi Yes Pratap
ReplyDeleteFirst thanks for sharing beautiful article.
Here i have one question in my mind is like
Can you please explain me what is actual use of annonymous class in java and in which situation we required to implement that?
Brother , spelling of developer is wrong in your "About Me" , thought you should correct it.
ReplyDeletethanks bro.. corrected it :)
ReplyDeleteHi Winner,
ReplyDeleteit's preferred to use inner classes if we are using that piece of code in only one place. Writing a class is not required in this case. So directly we are implementing the interface/abstract class with help of anonymous class.
For more information refer https://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html
thanks....
ReplyDelete
ReplyDeleteAfter reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in
selenium training in Bangalore
selenium training in Marathahalli
selenium training in Btm layout
selenium training in Jaya nagar
selenium training in Electronic city
selenium training in Kalyan nagar
Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
ReplyDeletePython Online training
python Course institute in Chennai
Python Course institute in Bangalore
That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
ReplyDeletePython Online training
python Course institute in Chennai
Python Course institute in Bangalore
Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
ReplyDeletecontribution from other ones on this subject while our own child is truly discovering a great deal.
Java training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
These technical details had been of great help. Thanks for sharing!!Keep updating new posts on your blog!!
ReplyDeleteoracle training in chennai
oracle training in annanagar
oracle dba training in chennai
oracle dba training in annanagar
ccna training in chennai
ccna training in annanagar
seo training in chennai
seo training in annanagar
Really Very Informative Post , Thanks For Sharing The Information With Us.
ReplyDeletehadoop training in chennai
hadoop training in tambaram
salesforce training in chennai
salesforce training in tambaram
c and c plus plus course in chennai
c and c plus plus course in tambaram
machine learning training in chennai
machine learning training in tambaram
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeletedata science training in chennai
data science training in omr
android training in chennai
android training in omr
devops training in chennai
devops training in omr
artificial intelligence training in chennai
artificial intelligence training in omr
Thanks mate. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also event marketing and How Blockchain Technology Can Influence the Event Industry
ReplyDeleteMmorpg oyunları
ReplyDeleteInstagram takipci satın al
Tiktok Jeton Hilesi
Tiktok Jeton Hilesi
antalya saç ekimi
takipçi
İNSTAGRAM TAKİPÇİ SATIN AL
Metin2 pvp serverler
INSTAGRAM TAKİPÇİ SATİN AL
smm panel
ReplyDeleteSMM PANEL
İS İLANLARİ
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi
Good content. You write beautiful things.
ReplyDeletesportsbet
vbet
mrbahis
hacklink
korsan taksi
taksi
mrbahis
vbet
sportsbet
çeşme
ReplyDeletemardin
başakşehir
bitlis
edremit
R6BV
kuşadası
ReplyDeletelara
sivas
çekmeköy
fethiye
FYTX5G
salt likit
ReplyDeletesalt likit
GLBS
kıbrıs
ReplyDeleteedirne
muş
trabzon
balıkesir
WMSE2X
Antalya
ReplyDeleteAntep
Burdur
Sakarya
istanbul
8JTQJ
Eskişehir
ReplyDeleteAdana
Sivas
Kayseri
Samsun
SQC
https://titandijital.com.tr/
ReplyDeletedenizli parça eşya taşıma
sinop parça eşya taşıma
artvin parça eşya taşıma
antep parça eşya taşıma
QL1C
kastamonu evden eve nakliyat
ReplyDeleteantep evden eve nakliyat
balıkesir evden eve nakliyat
erzincan evden eve nakliyat
ankara evden eve nakliyat
DSD16İ
22782
ReplyDeleteçanakkale telefonda sohbet
bedava sohbet chat odaları
ücretsiz görüntülü sohbet uygulamaları
sohbet muhabbet
balıkesir sesli sohbet sitesi
bingöl chat sohbet
ankara kızlarla canlı sohbet
kastamonu görüntülü sohbet kızlarla
görüntülü sohbet uygulamaları ücretsiz
FA7FC
ReplyDeletemobil sohbet odaları
mobil sesli sohbet
Çankırı Muhabbet Sohbet
adıyaman ucretsiz sohbet
görüntülü sohbet uygulamaları ücretsiz
bayburt rastgele sohbet uygulaması
Kars Ücretsiz Sohbet
Aksaray Kadınlarla Görüntülü Sohbet
görüntülü sohbet canlı
EDC06
ReplyDeleteBinance Neden Tercih Edilir
Instagram Beğeni Hilesi
Binance Referans Kodu
Btcst Coin Hangi Borsada
Twitch Takipçi Satın Al
Kripto Para Nedir
Pi Network Coin Hangi Borsada
Kripto Para Nasıl Üretilir
Binance Referans Kodu
FFBC8
ReplyDeleteArbitrum Coin Hangi Borsada
Twitter Trend Topic Satın Al
Yeni Çıkan Coin Nasıl Alınır
Osmo Coin Hangi Borsada
Binance Hangi Ülkenin
Twitch İzlenme Hilesi
Threads Beğeni Hilesi
Binance Referans Kodu
Binance Referans Kodu
fgfhbngfjhngj
ReplyDeleteشركة تسليك مجاري
Great and I have a neat proposal: What House Renovations Need Council Approval top home renovation companies
ReplyDeleteشركة تسليك مجاري بالدمام HzUdE8PEvh
ReplyDeleteشركة عزل اسطح بمكة haZVQUPdkm
ReplyDeletelmijFmKNwW
ReplyDeleteمكافحة حشرات W1ieK5GThO
ReplyDeleteشركة عزل مواسير المياه ببقيق Yx91cJqAg3
ReplyDeleteشركة عزل اسطح بخميس مشيط aOG69ak1XP
ReplyDeleteشركة صيانة افران
ReplyDeleteDwUGHXP