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

  • Home
  • SEARCH
  • 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 7308653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:35:14+00:00 2026-05-28T23:35:14+00:00

I have learned that we can’t instantiate an abstract class. But today i tested

  • 0

I have learned that we can’t instantiate an abstract class. But today i tested some codes and i feel confused about it.

package MainPackage;

abstract class abstractClass {
    abstract abstractClass a_function();
}

public class Src {
    abstractClass m;
    public abstractClass abstractClassTest() {
        return m.a_function();
    }
    public static void main(String args[]) {
        System.out.println("Hello world!");
    }
}

Here i create an abstract class abstractClass and return it in the abstractClassTest() function. And it is compiled successfully without errors! IMO before return something, the computer should create an object of that type. And here it should create an object of abstractClass before return m.function(),which i cant understand. i think that we cant instantiate an abstract class means that we cant create an object of an abstract class or we cant new a class(e.g. abstractClass m = new abstractClass() is illegal). But from codes above, it seems that we can create an object of an abstract class. how can it realize? For the code abstractClass m, what does the computer do when it sees the code?We cant say java has instantiate the abstract class m for the code abstractClass m? and if java dont instantiate the class abstractClass, how can it return the object of abstractClass in the code abstract abstractClass a_function();?

  • 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-05-28T23:35:14+00:00Added an answer on May 28, 2026 at 11:35 pm

    Yes, it should compile without errors. It would throw a NullPointerException on execution though, if you ever called abstractClassTest, because you never initialize the m variable to refer to an actual instance. In order to do so, you’d have to create a concrete class which subclasses your abstract one.

    For example:

    public class ConcreteClass extends AbstractClass {
        @Override AbstractClass a_function() {
           return this;
        }
    }
    
    public class Src {
        private AbstractClass m = new ConcreteClass();
    
        public AbstractClass abstractClassTest() {
            return m.a_function();
        }
        public static void main(String args[]) {
            new Src().abstractClassTest();
        }
    }
    

    Note that nothing in your code creates an instance of the abstract class. Just because you’ve got a variable of that type doesn’t mean that an object of that type has been created.

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

Sidebar

Related Questions

I have learned that in a class you can declare variables and methods. They
I have learned from various tutorial that If a client can reasonably be expected
I just learned (the hard way) that Java Component s can only have one
I have learned that browsers can only load a few files from the same
im using mvc framework and i have learned some techniques that help me with
I have 2 tables. One is a table with things that can be learned.
I have just learned that you can use a Thread pool for multi client
I have learned that a pointer points to a memory address so i can
I learned that to configure c3p0 pooling in hibernate, we can have write the
From this question I have learned that they are stored on s3 but we

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.