Regarding Abstract

Hi,

I have a doubt in the abstract class concept.

Please go thro the below lines
-------------------------------

**********************************************************
If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don't (and you may choose not to), then the derived class is also abstract and the compiler will force you to qualify that class with the abstract keyword.
****************************************************

My understanding is that if u have a class as abstract and some methods of that class as abstract and when ur inheriting that class, and making object of the derived class all the abstract method in the base class should have method definitions was my understanding right?? if yes, please consider the below code

abstract class Instrument4 {
  int i; // storage allocated for each
  public abstract void play();
  public String what() {
    return "Instrument4";
  }
  public abstract void adjust();
}

class Wind4 extends Instrument4 {
  public void play() {
    System.out.println("Wind4.play()");
  }

I have base class now as "Instrument4" which is abstract and  importantly do note that i am having a method play as abstract, if i am going to make an object of the class wind4 which is inherited from the instrument4 i should provide the method definition for the play method(whichis a abstract method) in the base class right??  But amazingly this code is working without such need if that could be the case what the above lines meant.

Please clarify my doubt and correct me if i am wrong in my assumptions.

Thanks in advance,
Seena
seenaAsked:
Who is Participating?
 
heyhey_Connect With a Mentor Commented:
       If you inherit from an abstract class and you want to make
      objects of the new type, you must provide method
    definitions for all the abstract methods in the base class.

      that means that if you have abstract class and inherit it
      1. if you have implemented all the abstract methods from the base class the derived class is "OK" (not abstract)
      and you can make objects from the derived class.
      2. if you have NOT implemented all the abstract methods from the base class
      the derived class is still abstract and you can't make objects from the derived class.

      you don't implement abstract methods in the base class -
      you implement methods that are abstract in the base class in the derived class

      hope this helps ??
        heyhey

0
 
heyhey_Commented:
ok once again
in Java you have
classes - standard object concept
interfaces - these are only definions for some kind of behaviour (Printable - 'the one that can print)

and abstract classes - these are classes that have non-implemented methods

a beautifull example is ClassLoader
there you have all methods implemented - managing the loaded classes (hashtable) resolving class dependancies (almost VM level) etc. and the only method which is not implemeted is


 protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException

so the only thing you need to implement your own classLoader is to implement this method
its functionality is to load class data (as byte array) execute defineClass (which will check the data and probably define a class) and call resolve (which will check if this clas depend on some other classes and in such case call your loadClass again) ...

so
1. you can't instantiate ClassLoader (it's abstract)
2. you can get the current Classloader ( this.getClass().getClassLoader())
3. you can implement your own classloader by extending java.lang.ClassLoader and implementing only the loadClass method. than you can instantiate an object from your classloader class and use it to load classes (ClassLoader is abstract and your class is not because you've implemented all the abstract methods.)

hope this helps
  heyhey

0
 
seenaAuthor Commented:
Yep it works thank u for ur help hey_hey

Cheers
Seena
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.