Abstract Classes And Access Modifiers

Jan 21, 2015

I'm having difficulty understanding this small piece of code:

package food;
public abstract class Fruit {
private String color;
private String tasteType;
public void setColor(String color) {
this.color = color;
}
public abstract void setTasteType(String taste);
}

The above is an abstract class which describes the basic structure that every fruit should "extend".

The below is a concrete subclass of the Fruit class called Apple.

import food.Fruit;
class Apple extends Fruit {
public void setTasteType(String taste) {
tasteType = taste;
}
}

Also do note that the two pieces of code are in different packages!

Upon compiling the Apple class I get the following error:
Apple.java:4: error: tasteType has private access in Fruit
tasteType = taste;
^

What I don't understand is this: I've given a non-abstract implementation to the "setTasteType" method in the Apple class and clearly setTasteType should have the authority to modify the private instance variables of Fruit. But it turns out I'm wrong.

View Replies


ADVERTISEMENT

Why Are Final And Abstract Called As Modifiers

Feb 8, 2015

I just wanted to know that why are final and abstract called as modifiers ,what is the essence of calling them as modifiers since there are two types of modifiers access modifiers and non-access modifiers so final and abstract come under the second category ,so why are these called as modifiers?

View Replies View Related

Access Modifiers For Variables

Aug 15, 2014

Does a variable have public access modifier? if we can use it within the class and outside of the class then can i access a public variable as follows??

class mo
{
void display() {
public int a=9;
System.out.println(a);
}
public static void main(String[] args) {
mo m=new mo();
m.display();
}
}

ERROR:
It shows 6 errors :-O.
Error 1. illegal start of the expression
2. class,interface, or enum expected

View Replies View Related

Overriding - Use Public Access Modifiers With String

Apr 2, 2015

Why do we use public access modifiers with String toString() method in java while overriding???

View Replies View Related

Java Source Code File Naming With Access Modifiers

Feb 3, 2014

Why is this not valid in java:

Both uses public with the same class/interface name.

Test.java:
public class Test implements Test{
/// Some codes
}
public interface Test {
///Some methods
}

View Replies View Related

What Programs Use Abstract Classes

Sep 15, 2014

What programs use abstract classes?

View Replies View Related

Abstract Classes And Interfaces

Mar 29, 2014

I did research again....

interface:
methods - abstract, default, static ONLY(abstract methods have no body, while static and defaults do, right?)
fields - public, static, final ONLY
abstract class: a normal class, but has at least one abstract method
methods - all
i.e., static, non-static, abstract (can it have a default method?)
fields - all
i.e., public, protected, private / final, non-final / static, non-static

View Replies View Related

Interface And Abstract Classes In Java?

Jun 4, 2014

why don't I define my methods in a class, rather than going a level up and declaring it first in an abstract class/interface? If the point is to have different implementations for different needs, then we have the option to override the methods.

View Replies View Related

How Does Inheritance Work With Abstract Classes

Nov 19, 2014

I am writing small pieces of code to make sure I understand Java basics and I have the following.

package teams1;
public abstract class Team1{
private String sport = new String();
public abstract String getSport();
public abstract void setSport();
}
import teams1.*;

[Code] .....

It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1.

View Replies View Related

Interface / Abstract Classes And Loading Array

Oct 20, 2014

I am new to Java, and last week had an assignment to create a shopping list. I made it so that I have one class use a ProductData class to load an array of objects (description, price, priority). This week I need to take that program and change it so that it includes an Interface and Abstract Class. I need to also split one class up into at least 2 others.

I am having trouble getting my thoughts together and figuring out what to put in the interface and what to put in the abstract class. I'm thinking that it might be best to split up the ProductData class up into 3 different classes: description, price, and priority. Then have an interface with a print method. Each of those 3 classes will implement the interface.

As for the abstract class, have the price and priority extend the abstract class. The abstract class will be at the same level as the interface and contain the set and get methods. Right now they are of 2 different data types: int, double. Should I make both of them Double, and then use a method to change the priority to an int?

Should price and priority inherit from description, or should they all be at the same level? I am thinking that they should be at the same level because they all describe the item in the array.

My most confusing part is that I have no clue at all on how I can load that array when each object is split up in a different class. My professor went over ArayLists last week, and we can now use them if we want, but the assignment doesn't explicitly say that we should change it to an Array List. Where does the constructor for the ProductData() go? Do I split it up into 3 different constructors?

View Replies View Related

Error Using Abstract Classes - Cannot Find Symbol

Feb 2, 2014

I am stuck. It seems like I have done everything by the book but I keep getting the same error: cannot find symbol. The error is specifically addressing lines 9, 10, and 11 in the Alien class file. All that is supposed to happen is an output of information for the two types of aliens.

Java Code:

import javax.swing.*;
public class CreateAliens
{
public static void main (String[] args)
{
Martian aMartian = new Martian();
Jupiterian aJupiterian = new Jupiterian();
JOptionPane.showMessageDialog
(null, "

[Code] ....

View Replies View Related

Why Super Classes Always Declared As Interfaces And Not Abstract Class

Dec 1, 2014

While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,

Q1) My question is Why they declared the List as interface rather than Abstract class?

Q2) i read some site -

List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?

Answer required with detailed information for Q1 and Q2.

View Replies View Related

How To Access Methods From Other Classes Using Buttons In Javafx

Jun 4, 2014

I have a javafx class that has buttons and textfields. Here is the code for a button, and i want the button to make a new object but im having trouble setting the constructor

Label label = new Label("Type");
GridPane.setHalignment(label, HPos.RIGHT);
TextField textField = new TextField();

Label label2 = new Label("First Name");
GridPane.setHalignment(label2, HPos.RIGHT);
TextField textField2 = new TextField();

[Code] ....

after i create the object i will insert the object in an arraylist of person objects

View Replies View Related

Reading Class Files And Search For Access To Blacklisted Classes?

Oct 14, 2014

I am currently building a Plugin system for an application and I wanted to add a little security feature.

I have read many tutorials on this and they basically all came down to the same answer:

"Its complicated: You need a custom class loader and security manager and ... and even then its still tricky"

So I thought, before I load the classes dynamically I will simply read the class files. Why bother with a SecurityManager or ClassLoader if I can simply whitelist all allowed classes and search for all illegal Class access before I even load anything.

View Replies View Related

How To Import / Access Classes Created In One Package In Another Package

Apr 30, 2014

My working directory is e:ajava. in package p1 i create two classes c1 and c2. net beans creates three files e:ajavap1srcp1, e:ajavap1srcc1, e:ajavap1srcc2. package runs without a hitch. i create another package p2 under e:ajava. i want to use class c1 in p2. pray what on earth should be my import statement in the p2 source code after the first statement 'package p2'. another related querry what should i include in my class path so as to gain access to c1 and c2 in source code of p2.

View Replies View Related

What Are Benefits Of Using Interface Plus Abstract Class Over Just Abstract Class

Oct 8, 2014

What are the benefits of using an Interface plus an abstract class, over just an abstract class?

View Replies View Related

What Is Abstract Class

Jun 1, 2014

I am just started to learn java and i am facing trouble learning abstract class.

View Replies View Related

Constructor In Abstract Class?

Jun 23, 2014

Do we have constructor in abstract class? If we have then what is the use of it?

View Replies View Related

When To Use Interface And Abstract Class

Apr 17, 2014

I know whats the interfaces and abstract class and also know that difference between interface and abstract class,but here my doubt is eventhough abstract class more advantage than the interface,then why should we use interfaces and when?

View Replies View Related

Difference Between Abstract And Inheritance

Dec 16, 2014

I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...

View Replies View Related

Polymorphism Possible Only When Superclass Is Abstract?

Jan 23, 2014

I've a question regarding polymorphism.

Is polymorphism possible only when the superclass is abstract?

View Replies View Related

Reference Of Abstract Class

Aug 29, 2014

I passed my abstract class private final reference to another concrete class and I used abstract class reference as parameter to that concrete class constructor and in my main method and null to that parameter then only that program executes correctly...i placing my code below ..if there is any error tell me where is error occurring then i will check my code...i think my code is right but little bit doubt abstract class concept.

{
}
class concept1 extends concept
{
private final concept parent;
public concept1(concept aparent)
{
parent=aparent;
System.out.println(parent);
}
public static void main(String args[])
{
//concept p=new concept1(null);
concept c=new concept1(null);
}}

View Replies View Related

Abstract Method With No Implementation

Mar 6, 2014

An abstract method is a method with no implementation. So would like to know what is the purpose of calling it if there is no implementation?

View Replies View Related

How To Call Classes Within Other Classes

Apr 18, 2014

How do you call classes within other classes? Or can you only call classes through the main?

View Replies View Related

Difference Between Abstract Class And Interface?

May 18, 2011

Difference between Abstract class and Interface??

View Replies View Related

Why Can't Create Object Of Abstract Class

Jul 18, 2014

Why we can't create object of abstract class ,when we can create its constructor?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved