Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2012, 11:01 PM   PM User | #1
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
Abstract Classes

It's confusing how method classes can be instantiated, trying to figure out where they should be placed within the main method file. To understand abstract classes is one thing, but to extend on them is something that can be very tricky to work with. For example:
Code:
public abstract class theem
   {
       public abstract class Power
      {
      
         private Hiker jim;
          public Digit(int vb, int yt)
         {
            jim = new Hiker(vb, yt);
         }
      }
In relation, a non-abstract class must be used in order for an action to be run, for the reason being that the parent is abstract and its children are concrete subclasses?
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.
Taro is offline   Reply With Quote
Old 12-11-2012, 02:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I'm not sure exactly what it is you are trying to do here.
Subclassing abstracts that are nested are. . . a pain to say the least. I don't recommend doing it unless you need to. The problem is simply the scope.
Here's an example of that:
PHP Code:
public class theem
{
    public abstract class 
outer
    
{
        public abstract class 
Power
        
{

            public 
int[] jim;
            public 
Power(int vbint yt)
            {
                
jim = new int[]{vbyt};
            }
        }
    }

    class 
ConcretePower extends outer.Power
    
{
        public 
ConcretePower(outer outerint vbint yt)
        {
            
outer.super(vbyt);
        }
    }

    class 
ConcreteOuter extends outer
    
{
    }

    public static 
void main(String[] argv)
    {
        
theem t = new theem();
        
outer obj t.new ConcreteOuter();
        
outer.Power p t.new ConcretePower(obj14);
        for (
int i p.jim)
        {
            
System.out.println(i);
        }
    }

(I have no idea what these are doing, so I fudged my own compatible datatype for jim).

You'll notice that the nested abstracts are scoped against an instance of object theem (which I promoted to a concrete class and went with the new outer).

Perhaps its better to simply acknowledge the awesomeness that is the interface first which will give a much better understanding of the usefulness of the abstract. Ultimately the purpose here is to create multiple inheritance, where an object of my ConcretePower is both an instance of ConcretePower as well as an instance of theem.outer.Power. Therefore I can operate on it with any method provided in either of the classes.

As for placement in a file, I don't understand the problem. Nothing says you need to nest abstracts. Nesting in Java is already IMO a pain to keep track of everything.
Fou-Lu is offline   Reply With Quote
Old 12-11-2012, 08:54 PM   PM User | #3
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
Thanks Mark and Fou-Lou;

I kind of get this abstract stuff I'm learning in Computer Science; in addition, sometimes I had to make sure I was using the right declaration types because I use a file with the main method and a file with a bunch of class methods to refer to.

PHP Code:
class ConcreteOuter extends outer
    
{
   
 } 
I was thinking of taking this out of my snippet of code, as I do have an extension of a class in the main method or subclass file. Although it may change the function, I would have to try to actually include an implementation, since that this is not abstract class ConcreteOuter extends outer.
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.
Taro is offline   Reply With Quote
Old 12-11-2012, 09:47 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That one is not supposed to be abstract, it is supposed to be concrete.
You cannot instantiate an abstract class, so in order to demonstrate the extends and nesting I needed to create the two concrete classes. That is very important in order to show the instantiation of the nested classes.

The simplest way to look at the abstract is a skeleton shell. It doesn't know anything other than the absolute basics of what it should be representing. If you have an animal for example ('scuse the example my biology classes are well over a decade behind me so terms are fleeting) you could track the Kingdom, but that's about it. You can then superclass that to another class which may be concrete or abstract which represents the phylum or class, and work down the chain of refinement until you get to the species (which is the bottom so now you need to actually use a concrete). You can implement as many concrete or abstract methods as you want down the chain. Most of these can be implemented to the top level abstract Animalia, so at minimum you'll be able to get the kingdom, phylum, class, uhhh, order? and ultimately down to species. This means you can typecast against the highest abstract (or interface)(perhaps not a great example since you could just as easily use properties):
PHP Code:
public abstract class Animalia
{
    public 
String getKingdom()
    {
        return 
"Animalia";
    }
    public abstract 
Phylum getPhylum();
    public abstract 
Order getOrder();
    
//. . .
}
//. . .
public Phylum getAnimaliaPhylum(Animalia a)
{
    return 
a.getPhylum();

Since you know you cannot instantiate an Animalia directly, whatever is provided in Animalia for the getAnimaliaPhylum guarantees that you can fetch the .getPhylum on the instance of Animalia a.

One of the best usages of the abstracts is to create the base container for multiple interfaces. Take as many interfaces as you want to implement (but not constantly rewrite) and drop them into an abstract. Refine as needed.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
abstract, class, private, public

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:46 AM.


Advertisement
Log in to turn off these ads.