Reputation: 71
I am fairly new to the concept of abstract data types an was looking for clarification because I could not find any good examples online.
From my understanding, the sub class inherits all methods and variables from the abstract but I think I am misunderstanding this. For example, I am creating a menu using the abstract data type MenuItem
import javax.swing.*;
public abstract class MenuItem{
private String itemName;
private int ct;
private double costPer;
public String getItemName()
{
return itemName;
}
public int getCt()
{
return ct;
}
public double getCostPer()
{
return costPer;
}
}
public class Hamburger extends MenuItem{
itemName = "Hamburger";
ct = 0;
costPer = 4.99;
}
I know this is incorrect but can someone tell me why? Does the subclass hamburger only inherit the methods or what?
Upvotes: 1
Views: 2053
Reputation: 3163
From my understanding, the sub class inherits all methods and variables from the abstract but I think I am misunderstanding this
yes, your understanding about your mis-understanding is correct. :-)
sub classes in java do not inherit the private member variables. they get public and protected members only.
Upvotes: 1
Reputation: 2499
An abstract class can never be instantiated. Its sole purpose is to be extended. In an abstract class, if you specify atleast one method as abstract, then the whole class needs to be specified as abstract. An abstract class allows you to have implemented and unimplemented (abstract) methods all in the same class. If all methods in the class are abstract, then you effectively have a interface, and any variables declared in an interface are treated as constants. The variables in your question are not inherited as they are private to the abstract class. You must access them through the methods of the abstract class.
Upvotes: 0
Reputation: 10245
Yes, Hamburger
only inherits the methods. That's because they're public. If you made them private (like the fields) they wouldn't be inherited either. Here's how to fix the problems.
import javax.swing.*;
public abstract class MenuItem {
//To be visible to subclasses, these need to be public, package-private, or protected
protected String itemName;
protected int ct;
protected double costPer;
public String getItemName() {
return itemName;
}
public int getCt() {
return ct;
}
public double getCostPer() {
return costPer;
}
}
public class Hamburger extends MenuItem {
//These assignments need to be inside a block, like a constructor
public Hamburger() {
itemName = "Hamburger";
ct = 0;
costPer = 4.99;
}
}
Upvotes: 0
Reputation: 19797
Before you start looking into abstract types, start with the concept of encapsulation, and try to understand it as it is considered (by many) as the most important concept in the object-oriented design, followed by polymorphism, and inheritance. If class members are private, no subclass will be able to access them directly.
Upvotes: 0
Reputation: 61558
The problem lies in the visibility of the fields in your MenuItem
parent class. private
visibility means, that they are not visible to any other class including own subclasses.
In oreder to make your fileds visible to subclasses, you have to change their visibility to protected
. Be aware that this makes the fields visible to all classes in the same package as well.
All the memeber visibility issues are covered in greater detail in this article
Upvotes: 1
Reputation: 27880
itemName
, costPer
and ct
are declared as private access fields. They are only accessible from within the class they are defined in. If you declare them with protected
access, you'll be able to access them.
As defined in the Java Language Specification, section 6.6 Access Control
A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
- ...
- (Otherwise,) if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
- ...
Upvotes: 0
Reputation: 500753
There are several issues:
itemName
et al are private
, so even though they're inherited, they're not visible to the subclass.Hamburger
is invalid.Here is how you could fix your code:
public abstract class MenuItem{
public MenuItem(String itemName, int ct, double costPer) {
this.itemName = itemName;
this.ct = ct;
this.costPer = costPer;
}
...
}
public class Hamburger extends MenuItem{
public Hamburger() {
super("Hamburger", 0, 4.99)
}
}
Finally, I'd say that instead of using an abstract base class and a bunch of concrete classes, it would be better to use a single concrete class for MenuItem
and make Hamburger
etc instances of that class.
Upvotes: 2