Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to override abstract methods and use variables without making them public

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to override abstract methods and use variables without making them public

    So I have a class called student and one of the variables is tuition rate, i want to set the tuition in the other class but i dont want to make the variable public, (Its not that big of a deal at the moment but i would like to see if i could) is there a way to do it?

    Heres my first part of code from the abstract Student class
     
    public abstract void setTuition(double rateIn);
     
     
        public abstract double getTuition();
     
        abstract String Display()

    now heres where im trying to override in the Undergraduate class
     
    @Override
      public void setTuition(double rateIn){
           rateIn=200*super.getHours();
     
      }
     
     
     
     
      @Override
      public String Display(){
     
     
     
     
          return("name "+super.getLastName()+", "+super.getFirstName()+"\n"+
                             "ID "+super.getStudentId()+"\n"+
                             "Enrollment "+super.getHours()+"\n"+
                             "tuition $"+());
          //Here i need the variable so i can print it out
     
      }

    All i need is a way to print out the variable
    Last edited by Cheeseballs77; May 19th, 2021 at 08:00 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to override abstract methods and use variables without making them public

    is there a way to do it?
    Yes, use the get... and set.. methods to access and change the variable.

    way to print out the variable
    You need to have a reference to an instance of the class with the variable; then call the get... method for the variable:
      refToClass.getVariable()     // get the contents of the variable
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  3. Replies: 12
    Last Post: January 1st, 2011, 03:47 AM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM