|
There are some abstract DefaultWhatever classes in Swing. They implement all the methods required by the Whatever interface, but all the methods do nothing. You can extend this class to override any methods you need with less effort than implementing all the methods in the interface. It feels like cheating somehow, but it's very handy. |
Here's my guess: Suppose the class in question was a class named A. Suppose A extended another abstract class S, or implemented an interface I, but A did not provide an implementation for any methods in the superclass S or interface I, then the class A must be declared abstract. The fact that it provides a single non-abstract method is irrelevant, ... |
Abstract class is a class will have abstract methods and non-abstarct methods may also. If you want to use abstart methods, then the class should also be abstract. Generally abstract methods will have declaration part only. It must be implemented by the subclass which extends it. Ex: abstract class Super { abstract public void amethod(); abstract public void bmethod(); public void ... |
Originally posted by marc weber: The argument lists are different, so this should be fine. It works when I compile it. What error message are you getting? Must not be adding to what he already has then. In total he has: abstract public String processInput(String theInput, Socket socket); abstract public String processInput(String theInput, Object obj); abstract public String processInput(String theInput, Socket ... |
I am having a problem...I have to take a program I have already written and make it abstract. I thought I had it but it won't compile it doesn't like the following code..... public int compareTo(Employee anEmployee) { final int BEFORE = -1; final int EQUAL = 0; final int AFTER = 1; Employee temp = new Employee(); <------It doesn't like ... |
From your description so far it sounds like a single class would do the job. In a running program you could create any number of instances to represent different courses. With just that, I don't see any need for abstract methods. But, if your assignment included instructions to use some abstract methods in an abstract class you'll have to find some. ... |
|
|
@jo pure curiosity - why don't you try yourself? don't get me wrong, it's good to post questions here, but in order to find out the answer to a "can i ..." question, trying out is perfectly okay. if it comes to the "why..." part, people will be happy to help... many greetings, jan |
What is the basic difference between abstract class and an ordinary class in Java. The keyword "abstract" in class or method declaration. Other ordinary subclasses need to implement these abstract methods. What is the difference between an abstract class with all the methods defined and that of an ordinary class? Abstract classes can not be instantiated. Any attempt to instantite an ... |
I have an abstract class Shape, which will be extended by two other classes Circle and Square. I am to create an array of data type Shape that will hold objects of class Square and Circle. Arrays are objects, but how is it possible to make one of a data type that cannot be instantiated (because the class is abstract)? Can ... |
|
interface SomeVerb_able { // For example, *SomeVerb* may be say *Draw* ??... void someAction( ); } /* end Interface */ // For example, *SomeVerb* may be say *Draw* ??... // And *myAbstractClass* may be..umm..*Shape*...?? abstract class myAbstractClass extends SomeVerb_able { // Extra method specs...on top of those ALREADY in the Interface... } /* end Class */ |
|
I have a problem with the following;I request you to go through the code abstarct class X { abstract void a1(); abstract void a2(some parameter); // // } abstract class Y extends X { abstract void a1() { //some code; } some code... for a2() etc public static void main(String args[]) { How can I access a1() here?? } } I ... |
|
Abstract classes and methods allow a class designer to implement part of a class but force some other class to implement the rest. If a class is marked abstract you can't create an instance. So "new AbstractButton()" won't compile. We have to have another class that extends the abstract class and fills in some missing bits. So "new JButton()" is fine. ... |
|
|
There is no good reason to write an abstract class. The typical reason is code reuse, but there are better ways of achieving that. In the case presented, I'd make Abstractdemo be called FinalDemo, and be final, not abstract. Or, even better, make the method static, as it doesn't rely on anything from any instance. |
public abstract class Account { private int acctNumber; private int accBalance; public Account () { accBalance = 0; } public Account (int acctNumber, int accBalance) { setAcctNumber(acctNumber); setAccBalance(accBalance); } public int getAcctNumber () { return acctNumber; } public void setAcctNumber (int acctNumber) { if (acctNumber > 0 ) { this.acctNumber = acctNumber; } } public int getAccBalance () { return accBalance; ... |
|
Why not? What does the abstract keyword mean? Your observations match the definition given in the Sun Glossary. Editing later: Oops, maybe not! The definition of "abstract class" is not quite complete, I think. A class that has abstract methods or the abstract keyword seems to be what the compiler is telling you. Why would we ever make an abstract class ... |
Abstract class consists of concrete methods and abstract methods. Interface Consists of only abstract methods and final variables. public class animal public class Lion extends animal public class Cow extends animal Take an example of animal class. Take the method definition of eating contents. For Lion class, this will be meat. But for Cow class this will be only grass. Take ... |
Welcome to the JavaRanch gurpreetsingh , I tried compiling your example and it compiles. Both A & B are concrete because neither of them is marked with the "abstract" keyword, and neither A nor B needs to be marked as "abstract" because neither of them has any methods that are marked as "abstract". Please give me more details. Kaydell [ May ... |
Probably he wants to know why should an abstract class be allowed to extend a non-abstract class. Its because a concrete type may have abstract subtypes, for example, a concrete class Car, with full functionalities of driving a Car, can have abstract subtypes of Diesel driven Car or Petrol driven Car, or a CNG driven Car. Sid |
Hi, Googling for this will most likely be the best way to go as there is tons of information out there but this is a small reason. Abstract classes tend to be used when you have a bunch of classes that can extend the same types of thing e.g. you could have an Employee abstract class that holds the information common ... |
|
Hi All, I have a doubt regarding the excersise 1.1 page 18-K&B book(with my scenario) If i create the abstract class Fruit, say the class contains abstract method x() and non-abstract methods y() and z(), and if i make Apple class(abstract) extending the Fruit class. Then there is no way i can access the non-abstract methods of class Fruit from Apple ... |
|
|
Hi Manas, Abstarct classes are meant for 'abstracting'. means if some classes are having common behaviour, instead of writing evry time the same thing in each class, write that in one class and ask the other classes to use it[by making the classes as subclasses to the abstract class]. this is nothing but inheritance. To summarise: Use abstract classes when you ... |
|
|
This can be a bit confusing. You can't construct an abstract class directly. But it can have a constructor. This is so, as Rob points out, its constructor gets called either implicitly via constructor chaining or explicitly (via the super keyword) to allow for it to be constructed (and have values initialized or what have you). Take Rob's suggestion and try ... |
For the sake of the implementation of non abstract methods in an abstract class. Let's say you have your abstract class: public abstract class MyAbstract { protected abstract int firstConcreteMustOverride() ; public int iDoSomethingUsefulThatYouInherit() { if( doFirstImportantThing() ) { return doSecondImportantThing() ; } return 138 ; // Magic numbers are bad..mkay } private boolean doFirstImportantThing() { // incredibly complex long code... ... |
Because an Abstract class is incomplete. See this example- abstract class A { abstract void display(); } class MainClass { public static void main(String[] args) { A obj = new A(); //(1) obj.display(); //(2) } } Now statement (1) will generate an error in Java. But if it had not, then what would happen at statement (2)? The call will try ... |
|
|
Hi All, I have a current design which is something like this. Abstract class --------------- public abstract void method1(Model obj); public void method2(Model obj) { *** method2 logic goes here**** } --------------- All sub classes inheriting the above abstract class will provide a custom implementation for method1. method2 is dependent on completion of method1 as the Model obj is first worked ... |
Hi there..i am working on a program where i am adding few widgets(components) on the panel. Now i could add a button to the frame, and also add a panel carrying a circle widget. but when i try to add a image on the panel and in turn on a frame, i get the error as : java.awt.Image is an abstract ... |
|
|
Hi Pillay, Abstract class are those class which may contain abstarct methods, and abstract methods are those which don't have definition. example- abstract class Person { int getHeight(); // note here -- no {} } class Pillay extends Person { int getHeight() { System.out.print("Pillay's height is ??" ); } } Since abstract class is partially implemented so it can't be instantiated. ... |
Originally posted by Campbell Ritchie: The only way you can get shorter than that would be to miss out the new line! You've done it! THANK YOU, CAMPBELL! Learning Java reminds me of my first year in med school. It's like learning a whole new way of thinking and a new language all at the same time! So, let me see ... |
|
|
No, an abstract class does not have a constructor because it is never instantiated (made into an object). There is nothing to "construct." The abstract class just provides default methods and variables for any subclass to inherit. Nothing happens to the abstract class when a subclass is instantiated, you just automatically have all the methods* and variables* when you extend the ... |
Surprisingly, it is. Apparently the compiler puts all initializations that occur when declaring in source code, into all constructors. For instance: public class Test { private int x = 10; public Test() { System.out.println("x"); } public Test(int y) { System.out.println("x"); System.out.println("y = " + y); } }// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.kpdus.com/jad.html // ... |
I have a public abstract class in a separate package. A class in default package has to extend it.And object is created for the sub class.The main is found in default package. but i get error. package student; public abstract class Engineering { public int mark=89; public void selection() { System.out.println("marks for Engineering -----> 88"); if(mark>=88) { System.out.println("got seat"); } else ... |
|
|
Hi S, An abstract class is a class that cannot be instantiated, and may contain either abstract or concrete methods. Since it cannot be instantiated it is expected to be extending by other classes so they can override its methods. Using a grade example we see: public abstract class GradedActivity { private String activityType; private int score; public GradedActivity(String activityType, int ... |
we declare a class abstract for two reasons 1)to prevent creting objects 2)if the class has any abstract methods if the class has abstract methods then the class that inherits it must implemet the methods that are declared abstract or declare itself abstract.... Hence if you want to force a sub class to implement the super class method the method must ... |
Abstract classes do not have to be purely abstract, in other words an abstract class can have implemented methods, as well as fields. These methods and fields of course will be inherited by all concrete child classes. This allows you to guarantee that specific implementations and properties (data) are available in the class hierarchy. If you don't need this, use an ... |
|
I'm reading about abstract classes, but I'm not sure if I understand them. Perhaps I'm just slow today, I feel I should totally get it. I understand you don't instantiate them. Then is an abstracts class use only for subclasses to inherit stuff from.. Should it be used in a situation where it is uncessary to have a normal parent class, ... |
|
Suhas is right. You can call abstract methods of an abstract class without problems. This is because you can only instantiate concrete classes, and concrete sub classes of abstract classes have implemented the abstract methods. Consider: abstract class A { abstract void print(); } class B extends A { void print() { System.out.println("I am B"); } } public class Test { ... |
Route object will have one constructor and some methods init here is the code Route.java package traffic; import java.util.*; import traffic.object.*; public class Route { private ArrayList m_route; private int m_indexOfDestination; public Route(int initialCapacity) { m_route = new ArrayList(initialCapacity); } public void add(MotionlessObject ml) { m_route.add(ml); } public void add(int index, MotionlessObject ml) { m_route.add(index, ml); } public MotionlessObject get(int index) ... |
|
He's definitely right. Java has always been this way. Or at least since the first language specification; I confess I'm not so familiar with what came before. An example of a use for this is KeyAdapter - or any of the abstract ***Adapter classes that implement EventListener. All three methods of KeyAdapter are concrete, not abstract. And they all do nothing ... |
Abstract classes can't be instantiated(you can't create objects of their type), but they can be subclassed. Which means that another class could inherit all of its methods and attributes; whether those methods are abstract or not is up to the designer. Example: Assume Java doesn't actually have a Math class abstract public class Shape { final double PI = 3.14; } ... |
Hi i'm trying to use netbeans for using the system that draw gui without writing code, i did my dialog, now i'm preparing classes but i get an error package desktopapplication1; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.*; /** * * @author Antonio */ public class TbEvent implements ActionListener { private JButton addButton; private JButton editButton; private JButton searchButton; private ... |
|
One use I have seen to enforce template design pattern. We can fix the sequence of method call using abstract class. Suppose we have a abstract class like the following is present public abstract class MySeq { public final void printHead(){ // code to print head } public final void printFoot(){ // code to print foot } public abstract void printBody(); ... |
|
i have a abstract class abs1 i have a method add() in abs1; class a extends abs1 class b extends abs1 the add method was overriden in both classes a and b(both have add() method); i am creating a reference for abs1 like abs1 xx=new abs1(); if i call xx.add(), which add() method will get called? |
I am trying to understand how the java access modifiers are working and have a scenario I need help with. Is there a way to give access to the "name" variable only to the extending class (AbExtender) and not to the instantiating class AbTester? Private will not allow the getName2 method in the sub-class access. Protected will, but it also lets ... |
|
|
Hello Everyone, I have put all my .java files in the same folder. I am trying to overload a method and pass the required parameters in a different class and am getting following error when trying to compile my EmployeeFormat.java file C:\Java\AdvJava\Lesson3\Assign31>javac EmployeeFormat.java EmployeeFormat.java:24: formatObj(java.lang.Object,int,boolean) in GenericFormat cannot be applied to (java.lang.String) str=super.formatObj(emp.getEmplId()+"emplTypeWidth1"+"limitJL1 "); ^ EmployeeFormat.java:25: formatObj(java.lang.Object,int,boolean) in GenericFormat cannot be ... |
A class being declared abstract means that the designer of the class has judged that there should be no instance of that class, only instances of subclasses of that class. That judgement does not depend on whether there are any abstract methods in that class. Perhaps the class must be subclassed to have any real meaning, but the relevant methods, instead ... |
Hi guys, I'm new here. I'm trying to make an abstract class Point with concrete classes of Point1D, Point2D, Point3D, with a general method in Point to compute the distance between current point to origin despite its type of Point1D, 2D or 3D. I came up with this: public abstract class Point { private double distance; protected Point() { distance = ... |
// located in the East end package spital; abstract class Spital{ public Spital(int i){} } public class Mudchute extends Spital{ public static void main(String argv[]){ Mudchute ms = new Mudchute(); ms.go(); } public Mudchute(){ super(10); } public void go(){ island(); } public void island(){ System.out.println("island"); } } 1) Compile time error, any package declaration must appear before anything else 2) Output ... |
unlike interface, abstract class allows you to give default implementation to subclasses. abstract class Hostel { public abstract boolean isLunchNeeded(); public void todayLunch() { System.out.println("the same boaring rice"); } } public class MechanicalGroup extends Hostel { public boolean isLunchNeeded() { return true; } //here this guys take hostel food/default lunch from Hostel, so here you got the benifit of concrete method ... |
Welcome to CodeRanch, Kalyan Naveenan! Abstract classes can define abstract methods as well as concrete methods. Abstract methods must be implemented by the subclasses who extend the abstract class. So using this mechanism you can define more generic methods in abstract classes which are applied to subclasses as well, and still other abstract methods which needed to be implemented by the ... |
|
Hello, I have three classes. One is a DataSetRequestor with various fields filled in by the user. I have an abstract class called DataSet holdoing commonalities, with subclasses for each specific type of DataSet. I need to get some values from the DataSetRequestor class to the abstract class as initialization parameters. Any thoughts? Thank You |
I have a method in my Data Abstraction Object (DOA) Class that retrieves data (row by row)from the database. I'd to pass this data (see line 14) to the Controller Class (MVC) where the data will be processed. The Controller Class cannot have direct access because this will be break MVC Convention. Somebody recommended to create an API. I dont know ... |
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ReadXMLFile { public static void main(String argv[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean bfname = false; boolean blname = false; boolean bnname = false; boolean bsalary = false; public void startElement(String uri, String localName,String qName, Attributes attributes) ... |
|
|
Hi experts I have a problem that I couldn't able to figureit so I thought to post in a forum so the problem is I have one abstract class(class only abstract) not any of methods in this calss are anstract ,then I want to call some of methods in my own class which extends from abstract class.But the thing is that ... |
|
|
Hi i am using. XML Code: package bussinessprocess; import fuego.bis.svm.impl.SvmData.Feature; import fuego.lang.Time; import fuego.papi.Activity; import fuego.papi.CommunicationException; import fuego.papi.InstanceId; import fuego.papi.InstanceInfo; import fuego.papi.InstanceStamp; import fuego.papi.Process; import fuego.papi.ProcessService; import fuego.papi.ProcessServiceSession; import fuego.papi.OperationException; import fuego.papi.Task; import fuego.papi.collections.InstanceActivityPair; import fuego.papi.exception.InvalidVariableIdException; import java.util.Locale; public class Main extends fuego.papi.InstanceInfo { public Activity getActivity() { throw new UnsupportedOperationException("Not supported yet."); } public boolean isActivityCompleted() { throw new UnsupportedOperationException("Not ... |
I am new to java technology. I have one doubt in the following program. class Test{ public abstract void m1(); } while compiling i am getting like Test.java:1: Test is not abstract and does not override abstract method m1() in Test class Test{ ^ 1 error what does it mean. could you explain what the exact meaning of this compile time ... |
That code tries to print out the user (if it's an instructor). I don't see any courses in your code at all. Which method? Post up the full error message text. What method? I don't know, the only place in your code that courses are mentioned is in a text string that is printed. Perhaps if you explained clearly and concisely ... |
Hellooo every one. I am new to this forum. plz help me, i want to invoke method[ m1() ] from abstract class [ Test1 ] by creating object for Test2 class, where i ll be overriding the m1() method in Test2 class. I WANT OUTPUT : "hi" :-) here is the program: abstract class Test1 { public void m1() { System.out.println("hi"); ... |
If u wan't a class whose object can't be created , make it abstract class. Example ... shape is an abstract class where as Rectangle, Circle and Triangle r the concrete sub classes When I declare Shape as an Abstract Class ...(becoz Shape is unique so I make it abstract so that the sub classes can use the abstract methods in ... |
|
Hi all, I am writing a Java application that edits MIDI files. My knowledge on Java class libraries and concepts is a little rusty though, I'm not too sure how abstract classes and indeed, abstract methods would work. In specific, I am having problems with this class: MidiFileReader (Java Platform SE 6) Now, it's an abstract class and all of its ... |
|
|
I have created a abstract class called Pipe and then i have 5 different types of pipe. Each with values which differentiate it from the other types. Java Code: public abstract class Pipe { protected boolean InnerInsulation, OuterReinforcement; protected int Colour; protected String[] grades; } Java Code: public class Type1 extends Pipe { public Type1() { this.InnerInsulation = false; this.OuterReinforcement = ... |
Consider making validateOrder a true method -- with a valid return type, likely a boolean. Make it abstract and thereby force all subclasses to implement the method. By the way, you only have one abstract type here, so it makes no sense to state that you want to validate input with "each" abstract type. |
An interface is a definition of what a class should look like from the outside: use it for defining a type of class that will have multiple implementations. For instance in the '[Observer|http://en.wikipedia.org/wiki/Observer_pattern] ' pattern any class implementing the observer interface can be attached to the subject. In this way when implementing the subject there is no need to know how ... |
(1) thanks for the wall of code. (2) thanks for providing readers with the compilation errors. (3) why are you using camelcase for Class names? [*READ THIS*|http://mindprod.com/jgloss/abstract.html] the errors seem very straight forward to me: bankPresentation.java:34: cannot find symbol symbol : class bankSummary location: class bankPresentation bankSummary customerList[] = new bankSummary[10]; ^ bankPresentation.java:34: cannot find symbol symbol : class bankSummary location: ... |
|