It's been a rather long time since I've messed around with Java Abstraction and/or Interfaces, but I'm coming back to it now for a project and something is getting on my nerves. Below is a snippet of my code.
public class A {
private static String name = "None";
private static String description = "No description";
public A() {}
public A(User user) {
user.setData(this);
}
public static String getName() {
return name;
}
public static String getDescription() {
return description;
}
}
public class B extends A {
private static String name = "B";
private static String description = "This is B";
public B() {}
public B(User user) {
super(user);
}
}
public class User {
private A a;
public void setData(A a) {
this.a = a;
}
public A getData() {
return a;
}
}
When I use B.getName() I expect it to return "B" but it's instead returning "None".
Now I'm obviously doing something wrong, and searching around didn't help a bit. I'm fairly positive that this is possible someway, unless I'm getting confused with another language.
Could someone please point me in the right direction? Thanks.
You called the getName method on the class B. B doesn't have a static method called getName, so it looks for it in the superclass, A, which does.
Maybe you expect B's version of name to override A's? Variables don't get overridden. A is accessing the static variable name defined on A, that the method was originally called on B doesn't affect that.
Inheritance and static methods don't work well together. OO concepts like polymorphism rely on runtime dispatching, the word static should imply the opposite of that. With polymorphism the program works at a high level of abstraction, referring to the objects by a super type and letting the subclasses work out the details. With static methods you have to refer to the specific subclass you want the method called on, so you don't have that level of abstraction.
Welcome back to Java again.
You are using static variable in class A and B. These variables are associated with class instead of the objects.
If you change your method to get name from the User, it will work as you are expecting.
You need to override the method getName():
public class B extends A {
private static String name = "B";
private static String description = "This is B";
public B() {}
#Override
public static String getName() {
return name;
}
public B(User user) {
super(user);
}
}
The problem you are facing lies in the definition of the methods getName and getDescription: They are defined in class A as static members. This means that even when calling B.getName() the actual call is A.getName() and there the static member variable value of name is set to None.
When thinking about inheritance you have be careful what you declare as static. This has nothing to do with Interfaces or abstract classes.
public class A {
protected String name = "None";
protected String description = "No description";
public A() {}
public A(User user) {
user.setData(this);
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
public class B extends A {
public B() {
name = "B";
description = "This is B"
}
public B(User user) {
super(user);
}
}
public class User {
private A a;
public void setData(A a) {
this.a = a;
}
public A getData() {
return a;
}
}
With the protected keyword you can access the fields from the extending class.
See also:
http://www.javatpoint.com/static-keyword-in-java
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
A couple of things to note in your class :
name and description are static variables in both A and B
getName is a static method in A
static variables are bound to the class and static methods can't be overridden
This is the expected behavior since getName() method of class A has access to member variable of its own class that is "name" of class A. It is NOT because of name is static even if you make it non-static and you access it as shown in below code snippet it would return "None". Remember that only methods get overridden not member variables. So "name" of class B is not overriding "name" of class "A".
B b = new B();
System.out.println(b.getName()); --> "None" ("name" is non-static)
----------------------------------------------
System.out.println(B.getName()); --> "None" ("name" is static)
Also, if you want to get "B" as output , override getName() method of class A in class B and make method and variable non-static.
Related
When one object of a class has a reference to another object of
the same class, the first object can access all the second object’s
data and methods (including those that are private).
I took this sentence from a book. But I couldn't figure out actually what it means.
It means that private members are visible to other instances of the same class. For example:
class A {
private int v;
public boolean isSameV(A other) {
return this.v == other.v; // can acccess other.v
}
}
It means that if you have a class that looks like this
public class A {
private int number;
private A otherInstance;
public int number2;
public void DoStuff() {
...
}
}
you can access A.number in the DoStuff method (or any other class method) even although number is actually private.
e.g.
public class A {
...
public void DoStuff() {
this.otherInstance.number = 42;
^^^^^^^
cannot access private members here
}
}
is perfectly fine, while
public class B {
private A aInstance;
public void DoStuffToo() {
this.aInstance.number = 42;
}
}
would not compile, because B cannot access A's private members.
Good question actually, I faced similar problem when I started learning Java, here is how it looks in practice:
public class A {
private String example;
protected int anotherOne;
public A(){
}
public A(A a){
this.example = a.example; // here we get access to private member of another object of same class
this.anotherOne = a.anotherOne; // it works for protected as well
}
// This works for methods not just constructor, lets consider we want to swap value of example:
public void swapExample(A a){
String temp = a.example;
a.example = this.example;
this.example = temp;
}
}
Private fields can be accessed from inside of the class, by this construction you can access all the field of an instance of Foo without getters and setters when you are in class Foo :
public class Foo {
private String name;
public int sumLetter(Foo b) {
return this.name.length() + b.name.length();
}
}
The doc : Declaring Member Variables :
private modifier — the field is accessible only within its own class.
I am a little confused on how to set up the TestHomework method so that it prints properly when using the toString() method. Right now when I run the main method it prints "null - 0" but what I would like it to say is "Math - 6". This program is supposed to extend an abstract class. It is supposed to say how many pages there are for homework and for what subject.
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
{
// initialise instance variables
pagesToRead = 0;
typeHomework = "none";
}
public Homework(int pages, String hw) {
this.pagesToRead = pages;
this.typeHomework = hw;
}
public abstract void createAssignment(int p);
public int getPages() {
return pagesToRead;
}
public void setPagesToRead(int p) {
pagesToRead = p;
}
public String getTypeHomework() {
return typeHomework;
}
public void setTypeHomework(String hw) {
typeHomework = hw;
}
}
public class MyMath extends Homework {
private int pagesRead;
private String typeHomework;
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
public String toString() {
return typeHomework + " - " + pagesRead;
}
}
public class TestHomework {
public static void main(String[] args) {
MyMath one = new MyMath(6, "Math");
one.createAssignment(6);
System.out.println(one);
}
}
That's because you are defining the 2 properties (that one of them happen to have the same name as one of the abstract class's) but you are not initializing them, you are initializing those of the abstract class. (So their values is always set to their type's default)
You need to drop those from the MyMath class, & define the toString method in your abstract class: it's the one to be used by default by its inheriting classes.
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
// Same code
// Define the toString here
#Override
public String toString() {
return typeHomework + " - " + pagesToRead;
}
}
public class MyMath extends Homework {
// You don't need to define any extra attributes
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
}
public static void main(String[] args) {
// Calls the constructor of the MyMath class, which in turn
// invokes the constructor of its superclass, the 'Homework' class
MyMath one = new MyMath(6, "Math");
one.createAssignment(6);
// Invokes the toString of the MyMath class. Since it does not have one,
// The toString method of its superclass (Homework) is called.
System.out.println(one);
}
Your derived class has its own typeHomework and pagesRead fields, which are never set (even though the base class happens to have fields with the same names). Therefore, they stay null and 0.
You should delete those fields and use the data from the base class, via the public getter methods.
Why it doesn't work:
Be careful you redeclared the attribute typeHomework of you parent class. Attributes are automatically added to your extending class so you don't have to write them again.
By redeclaring it you confused the compiler, viewing your code in debug shows, that your one object contains your typeHomework twice:
typeHomework = null // The one from the super class
typeHomework = "Math" // The one from your child class
Your method now uses the typeHomework from your super-class therefor the output is null!
pagesRead is 0 because you are setting the pagesToRead of your super-class to 6(not pagesRead!) when calling setPagesToRead(p);.
Some style tips
Use the #Override annotation when overriding methods like this:
#Override
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
It's not really needed but it's good practice (readers of your code know that it overrides something).
When referring to attributes of your class it's also good practice to use the this statement so it's clear, that you're referring to an attribute and not a local variable:
#Override
public String toString() {
return this.typeHomework + " - " + this.pagesRead;
}
For an assignment, I have an ArrayList of type "Reference". Reference is a parent class to the "Book" class and "Journal" class. If I am allowed to add objects of type "Book" and "Journal" to the Arraylist, why would I be getting an error if I want to access methods of Book and Journal via the following code?
ArrayList.get([someindex]).someBookorJournalMethod()
The arraylist itself is of the parent class, and the methods I want to access are only defined for either book or either journal.
EDIT: Here is some code
public class Books extends Reference{
private String Authors;
private String Publishers;
public Books(String CallNum, String Author, String Title, String Publisher, int year,String type)
{
super(CallNum,Title,year,type);
Authors= Author;
Publishers=Publisher;
}
public String getAuthor()
{
return Authors;
}
public class LibrarySearch {
private ArrayList<Reference> Library;
public LibrarySearch()
{
Library = new ArrayList<Reference>(100);
}
public outputLibrary(){
for (int i = 0 ; i < Library.size(); i+++)
{
if (Library.get(i).getType().equals("Book"))
{
System.out.println("Type:book\n" + "Call Number:" + Library.get(i).getCallNumber() +"\n" + "Authors:" + Library.get(i).getAuthors();)
}
}
}
IntelliJ is having issues with the line Library.get(i).getAuthors() because it is a method specific to Books. How would I resolve this?
Because when you specify the type of a variable, you can only invoke methods that are defined for this type. For example, if you have
public class A {
public void methodA() {
System.out.println("A");
}
}
public class B extends A {
public void methodB() {
System.out.println("B");
}
}
public class Main {
public static void main(String[] args) {
A ab = new B();
ab.methodB();
}
}
This will not compile, since the type defined for the variable ab is A and the only visible methods are those that are defined in A.
In your case you can simply add a blank method in Reference(if you don't want to make the class abstract):
public void someBookorJournalMethod() {}
Or you can explicitly cast the object you're trying to invoke the method for.
However, it is important to note that both approaches are usually bad practices and should be avoided. If it does not make sense to instantiate Reference objects, than make the class abstract and define someBookorJournalMethod as an abstract method. In your code you're most probably using inheritance in a wrong way.
You can't access the method of a subclass from the superclass. So you'll need to cast to a Book or a Journal.
Reference r =ArrayList.get([someindex]);
if (r instanceof Book) {
((Book) r).someBookMethod();
} else if (r instanceof Journal) {
((Journal) r).someJournalMethod();
}
This question already has answers here:
Java Inheritance - instance variables overriding
(3 answers)
Closed 7 years ago.
I have 2 classes A and B such that
public class A {
public String a = "hey";
public void printA() {
System.out.println(a);
}
and
public class B extends A{
public String a = "Jude";
}
What do I need to do so that the output of the lines below is Jude
B object = new B();
object.printA(); //This should output Jude
You cannot access the subclass field from the superclass. However you can change it in subclass like this:
public class B extends A {
B() {
this.a = "Jude";
}
}
This way you don't declare the new field, but change the value of existing one. Note that extends A is necessary to specify that B is subclass of A.
Alternatively you may consider using a method instead of field:
public class A {
public String getA() {
return "hey";
}
public void printA() {
System.out.println(getA());
}
}
public class B extends A {
#Override
public String getA() {
return "Jude";
}
}
Note that in Java "variable" term usually applied to local variables declared within methods. You are speaking about "field", not "variable".
Fields can not be overridden. Methods can; use methods instead:
public class A {
public String getA() {
return "hey";
}
public void printA() {
System.out.println(getA());
}
}
public class B extends A {
public String getA() {
return "Jude";
}
}
That's all. If getA() is not called outside these classes, cincdider making it protected.
Just change your declaration of a to this.a like so.
public class B extends A{
B(){
super();
//Could hardcode this.a to "Jude" here if you want.
}
B(String word){
super();
this.a = word;
}
}
a is already defined for B from the superclass A so you need to use "this" to access it.
You can use it like so
B object = new B("Jude");
object.printA(); //"Jude"
The Java® Language Specification, Java SE 8 Edition
If the class declares a field with a certain name, then the
declaration of that field is said to hide any and all accessible
declarations of fields with the same name in superclasses, and
superinterfaces of the class.
Not sure If I need to further explain it for the audience, but YOU CANNOT "use values of local variables in inherited functions of java", why? See above.
I want to access fields of a class from base class in Java. I can do it in dot net.
see the example:
public class a{
// here I want to read the value of name or f1 or f2 or every other field values from derived classes
}
public class b extends a{
public string name;
}
public class c extends a{
public string f1;
public string f2;
}
How to do it?
You cannot read the fields your class doesn't own without explicitly naming the subclass. So, this is doable as follows:
((c)this).f1;
However, doing this would be a bad code smell: you are now tying an abstraction embodied by a to one of its specific implementations/extensions. You should better rethink your design.
An important note on code conventions
In Java it is a must that you name your classes using CamelCase and packages using lowercase, otherwise some quite bad name-resolution anomalies can happen. Not to mention any Java user getting totally lost reading your code.
You really don't want to do that as it defeats the idea of inheritance.
You can, however set up abstract functions that are implemented by derived classes. That's good programming style. Those functions can access member data in the derived and base classes.
Doing things like (i) using reflection and(ii) casting to derived classes are hacks and should be avoided. The reason being that changing a derived class should not trigger the necessity for changes in a base class.
What you should do in this case is to define abstract methods in your class a, which class b and c has to implement. You can then call these methods from a to obtain the values set by b and c.
public abstract class a{
// here I want to read the value of name or f1 or f2 or every other field values from derived classes
abstract String getName();
abstract String getF1();
abstract String getF2();
}
public class b extends a{
private String name;
#Override
public String getName() { return name; }
#Override
public String getF1() { return null; }
#Override
public String getF2() { return null; }
}
public class c extends a{
public String f1;
public String f2;
#Override
public String getName() { return null; }
#Override
public String getF1() { return f1; }
#Override
public String getF2() { return f2; }
}