Solved

Regarding Abstract

Posted on 1998-11-10
3
276 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 63
word0 challenge 3 58
word0 challenge 4 53
eclipse java  build path 6 20
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now