Abstract Class Constructors Are Not Recommended
Aug 28, 2014
in abstract class constructors are not recommended since we don't call it directly ...my doubt is below code is right or wrong...
3public abstract class Concept
4{
5 private String id;
6
7 protected Concept( String anId )
8 {
9 if ( anId == null )
10 {
11 throw new NullPointerException( "id must not be null" );
12 }
13
14 id = anId;
15 }
View Replies
ADVERTISEMENT
Oct 8, 2014
What are the benefits of using an Interface plus an abstract class, over just an abstract class?
View Replies
View Related
Feb 9, 2014
I remember reading that a super() call to parent no-argument constructor is automatically inserted by compiler. So, if i have a chained hierarchy of classes (starting at top, with Object), will there be a chain of super() calls, going from bottom to top in the chain ? Will a super() call be inserted in child, if i provide a no-argument constructor for this class ?
View Replies
View Related
Mar 10, 2014
class Test3 {
} class MySub extends Test3 {
}
class Test4{
public static void main(String args[]) {
MySub m = new MySub();
}
}
I learned that if a class and its parent class both have no constructors, the compiler is supposed to complain. When I compiled Test4, i got no errors. why did it give no errors?
I read this article : [URL] ....
View Replies
View Related
Jan 6, 2015
MyStack class have by default some fixed size of maximum elements, allow user of your class to specify in constructor what this maximum size is. Also add possibility to specify name of the stack in constructor. User can either create object without parameters, can specify only size or name, or both of them. And also override function toString(), that this code will print:
[stack1] 4 6 1
[stack2] empty
MyStack s1 = new MyStack("stack1");
MyStack s2 = new MyStack("stack2");
s1.push(4);
s1.push(6);
s1.push(1);
System.out.println(s1.toString());
System.out.println(s2.toString());
You will need in total 5 constractor: default constructor (new Stack())
1-parameter constructor precising maximum size of the stack (new Stack(10))
1-parameter constructor giving the stack name (new Stack("My Stack"))
2-parameter constructor giving the maximum size and the name of the stack (new Stack("My Stack", 10))
"copy" constructor (Stack s1=new Stack(); Stack s2=new Stack(s1);)
View Replies
View Related
Mar 27, 2014
I have to create an application that deals with maps.
I first have to create the instance variables for the class.
So very simply if my hashmap is going to consist of football clubs and players. Football clubs being a string value for the key and players being a set of strings for the values. How would I go about creating the instance variable in a class for this?
I can't seem to find anything that specifically deals with instance variables and constructors for maps.
View Replies
View Related
Jun 1, 2014
I am just started to learn java and i am facing trouble learning abstract class.
View Replies
View Related
Jul 22, 2014
Write a class Month that represents one of the twelve months of the year. It should have three attributes for
the name of the month,
the number of days in the month, and
the birthstone.
Also add constructors and getter/setter methods to access the attributes.
You may use the following code to test your class.
Java Code:
import java.util.*;
public class Month
{
// ADD CODE HERE!!!
public static void main(String[] args)
{
Month[] months = new Month[12];
[Code] ....
So what I have added so far is (under public class month { :)
Java Code:
String monthName, monthBirthStone;
int monthDays;
public Month (String name, int days, String birthstone)
{
monthName = name;
monthBirthStone=birthstone;
monthDays=days;
} mh_sh_highlight_all('java');
So I believe that is the constructor. I still do not understand several things:
What would I need the getter and setter for?
I tested it using just the above code, and using month 1 I got:
Month@5a1cfb56
This makes sense as I obviously didn't do anything in order to get it in a String format for the array. But I do not understand this still - how would I get the constructor to output a string (to then be in the array?)
View Replies
View Related
Jun 23, 2014
Do we have constructor in abstract class? If we have then what is the use of it?
View Replies
View Related
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
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
May 18, 2011
Difference between Abstract class and Interface??
View Replies
View Related
Jul 18, 2014
Why we can't create object of abstract class ,when we can create its constructor?
View Replies
View Related
Feb 21, 2015
I am under the assumption that In the return statement of getReciprocal() method(of the following code), a temporary Number object is created to hold the result of the calculation.
My question is, Number is an abstract class and we are only able to create reference of an abstract class not an object. But then how a temp Number object is created and returned?
class Gen<T extends Number>{
T ob;
Gen(T ob){
this.ob = ob;
}
Number getReciprocal(){ // Number is abstract class
[Code] .....
View Replies
View Related
Jul 23, 2014
I'm learning about abstract classes and I have to create an abstract auto class with make and price of a car, then two classes with a different type of car, and finally a main to use them. Everything seems to work and when I run it it's fine but I do get an error on the main that I'm not using the local variable buick1 and acura1.I'm curious because, while it runs for me, I want to make sure I'm doing it right and don't know of another way to do the output than this. I've put all four classes but the issue is on the last one (5 and 7).
public abstract class Auto
{
protected String makeCar;
protected double priceCar;
public Auto(String newMake)
{
makeCar = newMake;
[code]....
View Replies
View Related
Oct 5, 2014
Animal ob1=new Lion();
Here we create ob1 for Animal class or Lion class.
public abstract class Animal {
public abstract void eat();
public void breathe(){
[Code] ....
View Replies
View Related
Feb 4, 2015
Below Code
abstract class A
{
A(int a, int b)
{
}
}
If we can't create objects for abstract class, what is the need of writing constructor???
View Replies
View Related
Jul 8, 2015
What this interface inside that abstract class does. Looking for some examples to how can i use it ....
public abstract class Expression {
public abstract String toString();
public abstract SimpleExpression evaluate();
public void show() {
System.out.println(this + " = " + evaluate());
[Code] ....
View Replies
View Related
Oct 10, 2014
I've got an abstract class
public abstract class AbstractClass
{
//stuff
}
And a few classes that inherit from it
public class Class1 extends AbstractClass
{
//stuff
}
public class Class2 extends AbstractClass
{
//stuff
}
within another class I have a private variable with the type of the Abstract class, and within one of the methods I assign an object to the the variable like this:
public class Test
{
private AbstractClass temp;
public testMethod(){
Class1 anObject = new Class1();
temp = anObject;
}
}
Is this legal? Will temp become a Class1 object?
View Replies
View Related
Mar 12, 2015
How do I create an instance of a class in a method?
I am a bit rusty whenever I think of instances. I always think of main method and objects when I see instance which gets me confused on what to do when I am not in a main method. The example:
I have a abstract class, School, and inside School I have some methods that must preform some action on an instance. For example, there is a move() method that must move the instance of School. Another method named, personOld(), which returns whether or not an instance of School surpassed some determined age.
How do I do this and create this instance?
View Replies
View Related
Mar 7, 2014
I have a simple classes here one is interface and another one is abstract class when i try to compile them abstract class is givving compilation error.
public interface MyInterface{
public void getName();
public void getName(String s);
}
public class HelloWorld{}
abstract class SampleClass{
[code]....
View Replies
View Related
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
Oct 27, 2014
This code is directly from Swing: I'm using Eclipse and keep getting an error on line 10 saying :
"The type JTextField must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)."
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
[Code] ......
View Replies
View Related
Apr 6, 2014
Can you give me a simple description of what overloading a constructer is?
View Replies
View Related
Sep 1, 2014
Can i use constructors in an interface?
interface AI {
public abstract AI();
public abstract void hello();
}
Output:
I got the error as the method AI() should have return type.
View Replies
View Related
Jun 3, 2014
Is it possible to combine two classes that I have defined to contain some of the same elements so that NetBeans stops giving me errors? I don't want to get rid of any necessary code, and if both classes are necessary, should I just rename one of them? One class is an ArrayList that I am using to write the information for employees entered to a text file "employee.txt." I also want users to be able to call on this information via employeeID in order to display employee information. The code is the following:
public ArrayList<Employee> getEmployees() {
// if the employees file has already been read, don't read it again
if (employees != null)
return employees;
employees = new ArrayList<>();
if (Files.exists(employeesPath)) // prevent the FileNotFoundException {
[code]....
The other class is a getEmployee class that I previously defined before attempting to read the information from the text file and display it in the console. It is as follows:
private void getEmployees() {
try {
// if the file doesn't exists, create it
if (!Files.exists(employeesPath))
Files.createFile(employeesPath);
BufferedReader in =
new BufferedReader(
new FileReader(employeesFile));
[code]....
View Replies
View Related