Search code examples
javaoopabstract-classabstraction

Inserting Values to Non Abstract Methods in Abstract Class Using a Sub Non Abstract Class


I want to access and use 3 private variables in an Abstract Class(MainAbstract.java) from another class that has extended (SubAbstract.java) from the previously mentioned Abstract Class.

From the sub class I want to access the getters() and setters() of the main class's.

In the main class (this is an abstract class) there is an abstract method called ShowInfo().

This ShowInfo() abstract method should do something to view the each instance of the subclass's.

Below is the source code for the MainClass(Abstract) and the Sub Class SubAbstract. Please refer them.

MainAbstract.java

package abstractionexample;
public abstract class MainAbstract {

    private String sName;
    private String sType;
    private int iQty;

     public String getsName() {
        return sName;
     }

     public void setsName(String sName) {
         this.sName = sName;
      }

     public String getsType() {
        return sType;
      }

     public void setsType(String sType) {
        this.sType = sType;
     }

      public int getiQty() {
         return iQty;
     }

       public void setiQty(int iQty) {
           this.iQty = iQty;
       }

        public abstract void showInfo();

        public static void main(String[] args) {       

      }
  }

SubAbstract.java

package abstractionexample;

public class SubAbstract extends MainAbstract{

   @Override
    public void showInfo() {

    }

     //This is an instance and the getters() and setters() should use each    instance of this kind of to get values and set values.
     SubAbstract nSubAbs = new SubAbstract();  

 }

Solution

  • If I understand correctly, you want to use the setter methods to set properties of the instance nSubAbs and show these properties using the showInfo() method.

    The getters and setters are readily available to you in your subclass SubAbstract because it has inherited from the parent class MainAbstract

    Here's a code sample:

    class SubAbstract extends MainAbstract{
    
        SubAbstract nSubAbs;
    
        SubAbstract(int iQty, String name, String type) {
            nSubAbs = new SubAbstract();
            this.nSubAbs.setiQty(iQty);
            this.nSubAbs.setsName(name);
            this.nSubAbs.setsType(type);
        }
    
        private SubAbstract() {
            //no arg constructor
        }
    
        @Override
        public void showInfo() {
            System.out.println("iQty:" + nSubAbs.getiQty());
            System.out.println("name:" + nSubAbs.getsName());
            System.out.println("type:" + nSubAbs.getsType());
        }
    }
    

    And the main method of your MainAbstract class would look something like this (although this is a very bad place for the main method, but I suppose, you are trying to experiment)

    public abstract class MainAbstract {
    
        //..existing code..
    
        public static void main(String[] args) {
    
            SubAbstract subAbstract = new SubAbstract(10, "someName", "someType");
            subAbstract.showInfo();
        }
    }