Ask Experts Questions for FREE Help !
Ask
    kishorekumar's Avatar
    kishorekumar Posts: 21, Reputation: 1
    New Member
     
    #1

    Nov 12, 2006, 11:09 PM
    Java
    We can not instantiate an abstract class. But we can write a constructor in abstract class. If we are not able to create an object, then how and why is it possible to create a constructor in an abstract class?
    LTheobald's Avatar
    LTheobald Posts: 1,051, Reputation: 127
    Ultra Member
     
    #2

    Nov 13, 2006, 06:18 AM
    The definition of an abstract class is that it needs to have one or more abstract methods. It can also have a number of non abstract methods - hence why you can have a constructor. For example, the below is valid:

    Code:
    public abstract class QuickTest {
      public QuickTest() {
        // Constructor
      }
    
      // Abstract method
      public abstract void sayGoodBye();
      
      // Non-abstract method
      public void sayHello() {
        System.out.println("Hello World");
      }
    }
    kishorekumar's Avatar
    kishorekumar Posts: 21, Reputation: 1
    New Member
     
    #3

    Nov 13, 2006, 06:28 AM
    Quote Originally Posted by kishorekumar
    We can not instantiate an abstract class. But we can write a constructor in abstract class. If we are not able to create an object, then how and why is it possible to create a constructor in an abstract class?
    Thanks LTheobald for the reply...

    Yes. What u said is true. We can write. But I wonder what's the use of constructor. Now I've found that answer. We may be using private instance variables. So to initialize them we have to use constructor. An object is not created by the constructor. It's the new which creates. After creating object it executes constructor to initialize the variables.
    chowdari's Avatar
    chowdari Posts: 3, Reputation: 1
    New Member
     
    #4

    May 26, 2007, 02:41 AM
    Quote Originally Posted by kishorekumar
    We can not instantiate an abstract class. But we can write a constructor in abstract class. If we are not able to create an object, then how and why is it possible to create a constructor in an abstract class?
    Every class is responsible to initialize its instance variables.If subclass calls its abstract super class then it is the constructor that ini... the instance variables.
    Suddashil's Avatar
    Suddashil Posts: 3, Reputation: 1
    New Member
     
    #5

    Sep 24, 2007, 01:35 AM
    Abstract class is a class which can not be initialized but inheritated. Inheritance means properties of super class object is inheritated by sub class. So, object of abstract class is created for it's sub class only while inheritance mechanism takes place. So constructor is needed.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java [ 3 Answers ]

Please will you help me with these questions... where can I find a great site that will help me answer these... 1/ Who developed Java and when? 2/ What are some unique features of Java? 3/ Is java used for websites or stand alone programs? 4/ What does it mean when I say java is an "object...

Java Help! [ 2 Answers ]

Hi Experts, I'm not sure if I'm posting this in the right category, but I sure hope someone can still help me. I'm not able to view Java applets. I've downloaded the most recent version of JVM. I have version 1.4.1. I've tried changing some settings (like enabling Java in my internet...

Java Is used for [ 2 Answers ]

When I rest my pointer over a link, it diplays information about that link Is that Java at work? Why are there different Jav versions? Is Java compiled like C or interpeted like Basic? Is this contained in Internet Explorer? Thank You very much

By using vc++ or java? [ 1 Answers ]

Hi, I want to make image processing application.which programming language I should use? Either vc++ or java or anything else? Please guide me early.

Java Help! [ 7 Answers ]

Hi Experts, I'm not sure if I'm posting this in the right category, but I sure hope someone can still help me. I'm not able to view Java applets. I've downloaded the most recent version of JVM. I have version 1.4.1. I've tried changing some settings (like enabling Java in my internet...


View more questions Search