Nishtha patel
Nishtha patel

Reputation: 39

How to access the members of the concrete class using the reference variable of the abstract class

My abstract class has only three methods. My concrete class which extends the abstract class has price,name,packing. how can i access those properties using the reference variable of abstract class having instance of the concrete class. Directly i can't access any of the get set methods of the concrete class using this reference variable. Is there any special pattern for this problem???? According to my design, i have to assign the instance of the concrete class to the reference variable of the abstract class. I can't create the direct object of the concrete class.

Upvotes: 3

Views: 1073

Answers (3)

candied_orange
candied_orange

Reputation: 7308

My abstract class has only three methods. My concrete class which extends the abstract class has price,name,packing.

I take it these abstract class methods are not the same as the three concrete class methods. Fine, I'll call these unnamed methods the mystery methods. If it turns out they are the same then just delete the mystery methods you'll see below.

how can i access those properties using the reference variable of abstract class having instance of the concrete class.

By injecting the dependency.

Directly i can't access any of the get set methods of the concrete class using this reference variable.

If they only exist on the concrete class this is no surprise. The only reason not to add them to the abstract class as @JBNizet suggests is if you don't want them exposed to client code. This is a reasonable concern but there is a better way to handle it.

Is there any special pattern for this problem????

Patterns are solutions not problems. What you say you're doing is structurally close to the http://en.wikipedia.org/wiki/Composite_pattern but you have the composition going in the wrong direction. I don't think the composite pattern is going to be what you want here.

According to my design, i have to assign the instance of the concrete class to the reference variable of the abstract class. I can't create the direct object of the concrete class.

There is a way to achieve this without using instanceof and more importantly without the Abstract class even knowing that the Concrete class exists.

public class Driver {
    public static void main(String[] args) {
        Composition compositionInstance = new Composition( new Concrete() );
        System.out.println(compositionInstance);
    }    
}
public class Composition {
    Abstract abstractInstance;

    Composition(Abstract abstractInstance) {
        if (abstractInstance == null) { throw new IllegalArgumentException("may not be null"); }            
        this.abstractInstance = abstractInstance;//concrete assigned to abstract ref var            
    }        
    public String toString() {            
        return String.format( "Price: %s, Name: %s, Packaging: %s", 
                              abstractInstance.getPrice(), 
                              abstractInstance.getName(),
                              abstractInstance.getPackaging() );            
    }
}
public abstract class Abstract {        
    void mysteryMethod1() {}    
    void mysteryMethod2() {}    
    void mysteryMethod3() {}

    abstract String getPrice(); 
    abstract String getName();
    abstract String getPackaging();     
}
public class Concrete extends Abstract {
    String getPrice() {return "42";}
    String getName() {return "My Concret Product";}
    String getPackaging() {return "cardboard box";}        
}

A lot of classes but they are very simple. These class names are terrible so don't imitate them.

Driver doesn't have to touch the getters since it can let Composition do that for it. It only knows how to build Composition and Concrete. It doesn't get chatty with them.

Composition only has to know how to talk to Abstract. It has no idea how to build one or exactly what concrete instance it might get.

Abstract only knows about it self. Doesn't know anyone else. It doesn't want to know.

Concrete only knows about abstract.

By writing the code this way it is kept flexible. It should be easy to make changes as requirements change.

This wasn't written following any set pattern. What it follows are the S.O.L.I.D. principles.

Upvotes: 0

sosale151
sosale151

Reputation: 380

With a reference to the abstract class alone, you can't do much. However, you can type cast it to the corresponding concrete class.

eg: if you have class A and class B extends class A, you can do something like

if(obj instanceof B){
//type cast obj to class B here and call the respective method of B
((B)obj).concreteClassMethod();
}

Here obj is the reference variable of the abstract class A. This way, you can access any method (or public property) of class B, keeping the reference, however as class A.

Upvotes: 1

user4956851
user4956851

Reputation:

Example:

First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:

abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
    ...
}
abstract void draw();
abstract void resize();
}

Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extends GraphicObject {
void draw() {
    ...
}
void resize() {
    ...
}
}
class Rectangle extends GraphicObject {
void draw() {
    ...
}
void resize() {
    ...
}
}

than to acces to the father variable classes you can use: super.(variable) only if the variable is public or protected. If the variable is private, you will use get and set methods.

Upvotes: 0

Related Questions