need some clarification regarding abstract class.

27 views
Skip to first unread message

shweta sharma

unread,
Apr 3, 2013, 7:17:43 AM4/3/13
to bo...@googlegroups.com
Hi All,

I have little doubt in use of abstract keyword...

abstract classes cannt be instantiated . ( normally)
but this can be overruled by anonymous classes.
then what is the use of having abstract class?

like  abstract class classone{
     void mon();
}


classone co=new classone {

   void mon(){ do something };
}


it was asked in an interview & i couldn't give any satisfying answer..
we can always do it that way ...so what exactly is need ?
that is for sure that we can put common facility in single class & inherit  subclass to add specific implementation in its abstract method ...but that thing we can always do with normal classes .....


if anyone  can throw some light then it will be helpful..

Thanks
shweta

Mohammed Sanaulla

unread,
Apr 12, 2013, 8:33:48 AM4/12/13
to bo...@googlegroups.com
Creating Anonymous Inner classes is not creating an instance of the abstract class/interface. Anonymous class extends/implements the abstract class/interface on which the "new" is being invoked. 
In the given example new ClassOne is actually extending ClassOne and implementing/overriding the mon method.

--
You received this message because you are subscribed to the Google Groups "Bangalore Open Java Users Group- BOJUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bojug+un...@googlegroups.com.
To post to this group, send email to bo...@googlegroups.com.
Visit this group at http://groups.google.com/group/bojug?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Siddharth Krish

unread,
Apr 12, 2013, 8:59:42 AM4/12/13
to bo...@googlegroups.com
Hi Shweta,

When you create an anonymous class, you're doing exactly that; creating a class, it just so happens that this class doesn't have a name. in the example you're given above, your anonymous class will be forced to implement the method 'mon'. if you don't implement it, you'll get a compile time error.

The only step you're skipping is that you're not explicitly extending the abstract class. the anonymous class is free to override any concrete methods of the abstract class just like any other class extending the abstract class.

Since the class you've defined is an anonymous class, you're obviously restricted to the scope that applies to all anonymous classes.

hope this helps.

regards,
Siddharth
Lead Software Engineer
Simpa Networks
http://simpanetworks.com/work/



--
You received this message because you are subscribed to the Google Groups "Bangalore Open Java Users Group- BOJUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bojug+un...@googlegroups.com.
To post to this group, send email to bo...@googlegroups.com.
Visit this group at http://groups.google.com/group/bojug?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Siddharth Krish
Founder | Brick Oven
www.thehuckleberry.in

Ashok Renukappa

unread,
Apr 12, 2013, 9:04:12 AM4/12/13
to bo...@googlegroups.com
Going by the way anonymous classes behave, it creates a new class definition all together.

So in this case.

It creates a class called <EnclosingClassName>$1 as class name.
Where in <EnclosingClassName> is the class name in which particular code is present and it extends the real abstract class.

If you take an example like this.

public class AbstractTest {

    static abstract class ClassOne{
        abstract void implementThis();
    }

    public  static void main(String a[]){
        ClassOne classOneObj = new ClassOne(){
            void implementThis(){
                System.out.println("Implemented");
            }
        };
        classOneObj.implementThis();
    }
}

------------------------------------------

Actually it would do like this internally..


class AbstractTest$1 extends AbstractTest.ClassOne
{
  void implementThis()
  {
    System.out.println("Implemented");
  } 
}   

------------------------------------------



On Wed, Apr 3, 2013 at 4:47 PM, shweta sharma <shwe...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Bangalore Open Java Users Group- BOJUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bojug+un...@googlegroups.com.
To post to this group, send email to bo...@googlegroups.com.
Visit this group at http://groups.google.com/group/bojug?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Ashok R
Lead - Software Development
Verisign India Pvt Ltd.
Bengalooru.

Suresh

unread,
Apr 12, 2013, 9:13:39 AM4/12/13
to bo...@googlegroups.com
Anonymous class is a concrete class with no name. There is no abuse or overrule of abstract method here.
Example:
public class Test {
public static void main(String[] args){

ConcreteWithName namedClassObj = new ConcreteWithName();
namedClassObj.sayHello(); 

//With Anonymous
new AbsTest(){
public void sayHello(){ System.out.println("Hello Anonymous Class");}
}.sayHello();
}
}

abstract class AbsTest {
public abstract void sayHello();
}

class ConcreteWithName extends AbsTest {
public void sayHello(){ 
System.out.println("Hello Concrete Class");
}
}

In both cases, we are creating object using 'new' operator. It's just that one class has a name and other doesn't.

Hope this helps

~Suresh.


--
Reply all
Reply to author
Forward
0 new messages