inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zoellus
    New Member
    • Mar 2009
    • 2

    #1

    inheritance

    Is it possible to asign a value to a var while i a subclass when the data field comes from it's super class?
    I want to use it in another subclass to my super class.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by zoellus
    Is it possible to asign a value to a var while i a subclass when the data field comes from it's super class?
    There should be another verb in your question because now your question doesn't make much sense. Please rephrase your question. Maybe an example would help.

    kind regards,

    Jos

    ps. I suspect/guess the answer to your question is 'yes' but I'm not sure.

    Comment

    • zoellus
      New Member
      • Mar 2009
      • 2

      #3
      Code:
      public class Vm{
      protected String name;
      protected int price;
      }
      
      public class Select extends Vm{
      public void event(){
              super.name ="Myname";
              super.price=10;
         }
      }
      public class Payment extends Vm{
      public void printOut(){
      System.out.println(name+": "+price);
        }
      }
      Code:
      public class Main {
      
          public static void main(String[] args) {
      Select a = new Select();
      a.event();
      Payment b = new Payment();
      b.printOut();
          }
      
      }
      This is just a small example but I hope you understand what I want.
      My printOut will print null and 0 which is not what I want :(

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by zoellus
        Code:
        public class Vm{
        protected String name;
        protected int price;
        }
        
        public class Select extends Vm{
        public void event(){
                super.name ="Myname";
                super.price=10;
           }
        }
        public class Payment extends Vm{
        public void printOut(){
        System.out.println(name+": "+price);
          }
        }
        Code:
        public class Main {
        
            public static void main(String[] args) {
        Select a = new Select();
        a.event();
        Payment b = new Payment();
        b.printOut();
            }
        
        }
        This is just a small example but I hope you understand what I want.
        My printOut will print null and 0 which is not what I want :(
        Class Payment is another class than class Select; each object has its own 'name' and 'price', both inherited from their common super class Vm.

        Here's an example: both you and I extend from the (abstract) superclass Human. A Human has, say, a wallet. When they steal my wallet they didn't necessarily steal your wallet.

        kind regards,

        Jos

        Comment

        Working...