Solved

Regarding Abstract

Posted on 1998-11-10
3
277 Views
Last Modified: 2010-03-30
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
0
Comment
Question by:seena
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 30 total points
ID: 1227184
       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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227185
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
 

Author Comment

by:seena
ID: 1227186
Yep it works thank u for ur help hey_hey

Cheers
Seena
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
java stored proc example 9 44
Tagging and Merging on Branch 1 29
junit as external jar or library 7 22
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question