简体   繁体   中英

inheretance and using main method in its own class/package

So i have a paper publication class with protected variables and its own package.

Then i have a book class that extends paper publication. This book class then has 3 classes that extend book class (all three same package)

the book class has protected variables and the extended book classes have package private.

Now when i create a driver in a new class with its own package I cannot call any variable directly

package paperPublication;

public class PaperPublication {

protected String title;
protected double price;



package book;

import paperPublication.PaperPublication;

public class Book extends PaperPublication {
protected long ISBN;
protected int issueYear;


package book;

public class ChildrenBook extends Book {

int minimumAge;

Then my driver class which screws it all up... and i get why since its in its own package/class. but.... i cant seem to wrap my head around how to get this to work properly

package driver;

import book.*;
import paperPublication.PaperPublication;

public class Driver{
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Book a = new Book();
    System.out.println(a.ISBN);
    System.out.println(a.title);

isbn and title cannot be retrieved... make public. ;( which is not what i want. Also i have all constructors already.

the purpose of my project is to use protected,public,private,package private ints,strings etc... using inherentance and test the privacy by creating objects and calling straight variables like book.title or book.isbn from variables stored in constructors in a main method

You can't access private or protected variables like isbn or title directly from the Driver class. It simply won't compile. Accessing variables or methods outside their intended privacy scope won't compile. So that right there covers the privacy test you are looking for and serves as proof that access modifiers work.

If you actually want to be able to access private and protected variables from outside of their intended privacy, then you would use the design patterns mentioned below:

There's many ways (Design patterns you can use) to approach this. The conventional way would be to add getters and setters to a class to access its properties.

PaperPublication class:

package paperPublication;

public class PaperPublication {

   protected String title;
   protected double price;

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public double getPrice() {
      return price;
   }

   public void setPrice(double price) {
      this.price = price;
   }
}

Book class:

package book;

import paperPublication.PaperPublication;

public class Book extends PaperPublication {
   protected long isbn;
   protected int issueYear;

   public long getIsbn() {
      return isbn;
   }

   public void setIsbn(long isbn) {
      this.isbn = isbn;
   }

   public int getIssueYear() {
      return issueYear;
   }

   public void setIssueYear(int issueYear) {
      this.issueYear = issueYear;
   }
}

Driver class:

package driver;

import book.*;
import paperPublication.PaperPublication;

public class Driver {
   public static void main(String[] args) {
      Book a = new Book();
      a.setIsbn(123456789L);
      a.setTitle("Best book ever!");
      System.out.println(a.getIsbn());
      System.out.println(a.getTitle());
   }
}

Another design pattern would be to only have getters and you would set the value once, upon creation of the object:

PaperPublication class:

package paperPublication;

public class PaperPublication {

   protected String title;
   protected double price;

   public PaperPublication(String title, double price) {
       this.title = title;
       this.price = price;
   }

   public String getTitle() {
      return title;
   }

   public double getPrice() {
      return price;
   }
}

Book class:

package book;

import paperPublication.PaperPublication;

public class Book extends PaperPublication {
   protected long isbn;
   protected int issueYear;

   public Book(long isbn, int issueYear, String title, double price) {
      this.super(title, price);
      this.isbn = isbn;
      this.issueYear = issueYear;
   }

   public long getIsbn() {
      return isbn;
   }

   public int getIssueYear() {
      return issueYear;
   }
}

Driver class:

package driver;

import book.*;
import paperPublication.PaperPublication;

public class Driver {
   public static void main(String[] args) {
      // things are set in the constructor, and then you can only read, not change the values.
      Book a = new Book(123456789L, 2018, "Best book ever!", 19.99);
      System.out.println(a.getIsbn());
      System.out.println(a.getTitle());
   }
}

There are even more ways, it all depends on what your needs are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM