Link to home
Start Free TrialLog in
Avatar of seena
seena

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heyhey_
heyhey_

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

Avatar of seena

ASKER

Yep it works thank u for ur help hey_hey

Cheers
Seena