Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regarding Abstract

Posted on 1998-11-10
3
Medium Priority
?
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 90 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

597 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