Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:32:25+00:00 2026-06-17T22:32:25+00:00

Possible Duplicate: Interview : Can we instantiate abstract class? I have an abstract class

  • 0

Possible Duplicate:
Interview : Can we instantiate abstract class?

I have an abstract class with all its methods defined (i.e. there are no abstract methods contained within it) like the following:

public abstract class MyAbstractClass {
  String s;
  public void setString(String s) {
    this.s = s;
  }
  public String getString() {
    return this.s;
  }
}

There is also a JUnit test class:

public class TestClass {

  MyAbstractClass c;
  @Before
  public void setUp() {
    // What is happening here? And why does this work with an abstract class?
    // Instantiation? Extending the abstract class? Overwriting?
    c = new MyAbstractClass() { };
    // This will not work: (Why?)
    // c = new MyAbstractClass();
  }

  @Test
  public void test_AllMethodsAvailable() {
    // Why can I access the abstract class' methods?
    // Shouldn't they be overwritten? Or did I extend the class?
    c.setString("Test");
    assertEquals("Test", c.getString());
  }
}

I don’t quite understand why the assignment to c works in the first case but not in the second, or what is actually happening there (and as a consequence, why accessing the abstract class’ methods works in the test).

Can somebody please explain (and possibly point me to a Javadoc, article or book that explains why this works)?

Why can I “instantiate” an abstract class there? (Is that actually what I’m doing?)

Has it to do with inner classes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:32:25+00:00Added an answer on June 17, 2026 at 10:32 pm

    You are creating an anonymous inner class with that code. The class you create this way is implicitly extending MyAbstractClass.

    Since your abstract class does not have abstract methods you don’t have to provide implementations so this works.

    If you don’t know about inner classes you can check the official documentation which is quite nice I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a HTML
Possible Duplicate: Duplicate result Interview - Detect/remove duplicate entries I have a SQL Query,
Possible Duplicate: Qt equivalent of PathAppend? Is there a class that handles file paths
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: Interview Q: given an array of numbers, return array of products of
Possible Duplicate: Calling class method through NULL class pointer I was asked this question
Possible Duplicate: What’s the “condition” in C interview question? void main() { if(CONDITION) printf(Hello

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.