Why use an abstract class?

I am new to Java, and for some reason I can't get my head around why use an abstract class. I understand that an abstract class is something like:
public abstract class Food{ // abstract class

public void eat(){
// stub
}

}

public class Apple extends Food{

public void eat(){
// Eat an apple code
}

}
So basically the idea above is that you can eat a "Apple", but you can't eat "food" because you cannot instantiate an abstract class.

I understand what an abstract class is and how to write a. What I don't understand is why you would use it? It seems to me I might just create a normal class, called 'Food' and just do not instantiated. What are the benefits of using an abstract class?

807479 wrote:
If an abstract class should be used when you can implement fully some of the methods, but the other method can only be defined as heels?

Fix.

Note that Java abstract classes aren't necessary, but they add comfort and a way to correctly model the ideas we try to represent.

That is why it must be abstract because you can not instantiate the class because not all methods are completely formed?

Yes pretty close.

So basically in an example of a mammal, I might have a breath() method entirely defined in the abstract class because all breath of mammals.
But I should only do a method stub for speak() like any mammal makes a different sound.

Right.

Now I can not instantiate a mammal because it has only a stub for the speak() method so it must be an abstract class.

Fix. There is no common way speak all mammals.

But if I create a class called cow and extend the class mammal I only need to write the speak() method as I can inherit the breath() method completely trained.

Is this correct? I am an abstract class if I can't entirely make up together the method. But I use an abstract class when I can form some of the methods?

Your terminology is a little off, but, Yes, it seems that you have understood the concepts.

Tags: Java

Similar Questions

  • Why can I not use Get LV class Default Value in a dynamic VI?

    I'm trying to substitute a VI that uses 'Get default class LV value' and get an error that I do not understand.  My parent, 'Message class of ANT' class, has two children - "Class ANT from the command" and "ANT-class response.  Children participate a lot of data and features, including the factory pattern that implements 'Load Message class' VI of the superclass (see image).  I would replace this VI with a version of command and a version of the response, which would simply call the version of the Message with their respective classes of the substitution of dynamic input and output terminals.  However, I get the error "Front Panel"ANT Out class Message"Terminal: type of execution do not spread of dynamic input to dynamic output.»

    Don't know how to get around it.  Any ideas?

    Thank you

    -Jamie

    That sounds like a strange thing to do - what you will do is to return the default value for the entry class. What is the reason for the class loader of the disc? You SHOULD have already loaded the class in memory, because the output type MUST be the same as the input type (this is a requirement of dynamic distribution work, because the error is telling you). Since you already have the class that is loaded in memory, there is an easier way to get the default value: wiring a loop For iteration 0, and the entry class through wire directly to the output class, with tunnels on the inputs and outputs (NOT shift registers). As the loop never executes, the output is the default value for this class (see https://lavag.org/topic/15354-testing-for-the-same-class/#entry92502).

    Replace "to more specific class" with "class of Run-Time to preserve" will also work, but still, I can't understand why you try to do it at all.

  • Why my T3i keep on recording when you use a card class 10 SD video?

    With a speed class 10 SD card, my T3i will pull only photos, but stops in video.  I use 32 GB class 4 cards lately, just for photography.  The manual says to use a class 6 or higher card, SD when shooting video to reflect faithfully.  Does anyone have any suggestions on why its closing on me?  Thank you!

    He meant a bad quality of the map, no camera brand. 8 ^)

    Low level format you the card in the camera?

    Have you tried a different brand of card? I had no problem with my T3i.

  • When to use the interface and what Abstract Class?

    In a recent interview, was asked "when to use interface and what Abstract Class? Explain with an example.


    Situations in which a class should also end (example in real time)

    Have you tried to read this ? It is quite clear

    D.

  • Interface within an abstract class

    Hi all..

    I'm a little frustrated to understand what is this interface within this abstract class.

    Copy the following code is a part of the first question in our future review...

    I'll be happy to hear what you think, and if you guys could give some examples of how I can use it, that would be wonderful.

    public abstract class Expression {
      public abstract String toString();
    
    
      public abstract SimpleExpression evaluate();
    
    
      public void show() {
      System.out.println(this + " = " + evaluate());
      }
    
    
      protected static double toDouble(Object value) {
      return ((Number) value).doubleValue();
      }
    
    
      protected static interface Function {
      Object compute(Object... values);
      }
    }
    
    
    
    
    
    
    
    
    
    

    public class SimpleExpression extends Expression {
      private final Object value;
    
    
      public SimpleExpression(Object value) {
      this.value = value;
      }
    
    
      protected SimpleExpression(Function func, Expression... exprs) {
      Object[] values = new Object[exprs.length];
      for (int i = 0; i < exprs.length; i++)
      values[i] = exprs[i].evaluate().value;
      value = func.compute(values);
      }
    
    
      public String toString() {
           return value.toString();
      }
    
    
      public SimpleExpression evaluate() {
      return this;
      }
    }
    
    
    
    
    
    
    
    
    

    Thank you!!

    I'm a little frustrated to understand what is this interface within this abstract class.

    Not sure what you mean by "does". It defines only an interface.

    Do you mean why it is set inside the other class?

    This is done in order to limit the visibility of the interface. Note that it is marked protected.

    This creates a dependency between the enclosing class and interface so that they are used together.

    One use of (or members) interfaces is for better control of name resolution and prevent name clashes. Interface member names are qualified by the class name of the class that surrounds them.

    This ensures that any reference that see you to this interface is really the interface of the enclosing class and not a generic interface of the same name.

    If you create a class that extends an interface of the pubis, it is often possible for a person to usurp this public interface by creating their own interface and including in the classpath before the real interface.

  • I download Apple Quicktime for use in my class of biology to my PC - not a procuct apple how I can do

    I download Apple Quicktime for use in my class of biology to my PC - not a procuct apple how I can do

    Apple has removed QuickTime for Windows. Why do you need it?

  • can I use the generic class instead of delegates

    can I use a generic class instead of delegates?

    public void Sort (CompareDelegate, , cMethod) / / why we creat this "cMethod?
    {
    for (int i = 0; i)< data.length;="">
    {
    for (int j = i + 1; j)< data.length;="">
    {
    If (cMethod(data[i],data[j]) > 0)
    {
    T temp = data [j];
    data = data [i] [j];
    data [i] = temp;
    }
    }
    }
    }

    class delegate

    public delegate int CompareDelegate(T v1, T v2);
    main function
    numbers. ((v1, v2)-online v1 - v2 sort);

    This is the code works perfectly, but I have confusion about class generic and delegates... can we use this generic class instead of delegates by that logic...

    Hello

    The question you posted would be better suited to the MSDN Community. Please visit the link below to find a community that will provide the support you want.

    http://social.msdn.Microsoft.com/forums/en-us/categories

    Hope this information is useful.

  • Abstract class polymorphism

    Hello

    I am learning Java and ran through abstract classes and polymorphism. Let's say that

    ArrayList < run > run = new ArrayList < course >;

    LearnAbstractClasses to be an abstract method in the classroom. Let DerivedCourse come of course. If the LearnAbstractClasses method is not implemented in DerivedCourse, then DerivedCourse is also an abstract class.

    races. Add (new DerivedCourse()); What is this error step? My question is in the real-time scenario, I can initially be created DerivedCourse as a class concrete by implementing the LearnAbstractClasses method and it would have added current. For some reason any if I remove LearnAbstractClasses method DerivedCourse subsequently and is DerivedCourse as a class concrete, it won't have a ripple effect on courses.add (new step DerivedCourse())? I guess it's the ripple effect that OOPS is supposed to avoid.

    Kindly tell me if such a scenario is quite common in real time and if there is another way on this.

    RRSOra? wrote:
    Thank you. But my question was related to the real-time scenario, I've mentioned. To rephrase, did you ever face a situation in Java design where you had to convert a concrete class in an abstract class, and in this case you risk any knock-on effect in your code? In my example, what will happen to courses.add (new DerivedCourse()) if it is converted to a class specific to the abstract class? My knowledge of OOPS asserts that these changes to a class should not lead to changes to the code courses.add (new DerivedCourse()). Is it mandatory to ensure that courses.add (new DerivedCourse()) to remove?

    you are applying the characteristics of OOP. If I have an instance of course and call a method on it, so the code does not care what concrete subclass of course, he happens to use. If in the future, I refactor the hierarchy of classes of course and the actual instance to the execution of the changes, I have yet to edit the code.

    however, in your case, you are referring to the instantiation of a concrete instance of the course. It would be ridiculous to think that the code that actually cares what instance is instantiated will not need to change if you change the concrete classes in the hierarchy. That said, there are ways of abstraction which of more, if you need it. If you have code that needs to instantiate instances of classes, but does not need to know the ultimate DURATION implementation class, you can use the factory pattern. No matter what, you always come up with a piece of code (in this case, the implementation of the plant), who will have to change if you change the concrete classes in the hierarchy of classes.

  • Why we need Java class in c ++ pof serialization

    Hello
    I'm really confused why we need Java class that implements PortableObject to support complex c objects ++. If we do not use for questions or entry in the application processors, we cannot keep the object as a byte serialized format and we cannot recover in the deserialization of c ++.

    Please share your thoughts if there is a way, if we can skip any Java implementation.

    Kind regards
    Sura

    Try to make the default public constructor method in your class of the car.

    Like this

    Public Car()
    {
    }

  • The abstract class constructor

    Hello

    I can write a constructor for the abstract class, but I can't create the object. So, what's the point of this?

    Please guide me.

    Ankur Raiyani wrote:
    Hello

    I can write a constructor for the abstract class, but I can't create the object. So, what's the point of this?

    Abstract class can have instance variables. To initialize the instance variables of the class super (abstract class) of upper class, we use the constructor in the upper class (abstract class).

    Super() can be used here. For example,.

    public abstract class ClassA {
         int a;
         ClassA(int x) {
              a = x;
         }
    }
    
    public class ClassB extends ClassA {
         int b;
         ClassB(int x, int y) {
              super(x);
              b =y;
         }
    }
    

    Published by: Ram on July 29, 2011 16:03

    Published by: Ram on July 29, 2011 16:04

  • Why use Top Link is the best in the DB adapter?

    Hi all
    The one you suggest, why use Top Link (Build-in Insert, operation Select etc.) is the best in the DB adapter using the custom query above?

    Thank you

    Hi Michel,.

    to insert / select, it depends on what kind of user you are. TopLink allows you to browse and click on tables and have everything generated for you. If you're a DBA or show me the SQL type then you can simply type SQL directly.

    Some benefits of TopLink would be:

    -The range of SQL generated by TopLink is limited, but if you hard code SQL complex in your service you need to keep it.

    TopLink - can generate when executing the correct SQL for a database, to tell DB2 to Oracle easy.

    -L' merge operation will compare the XML of the columns on the data entry and update only what has changed. It can also make a sparse merger. If only have been 4 columns in the XML file, only 4 columns in the database will be updated.

    -For the incoming interrogation strategy (LogicalDelete, table of sequencing, etc.) is a configuration property and then at run time, several SQL statements are generated. Also leverages the SQL syntax advanced like the Oracle only for UPDATE SKIP LOCKED, he wrote everything yourself can be tedious and error-prone.

    -The main advantage of TopLink is when you go beyond the thinking on one table. If you import more than one table at the same time, TopLink will generate SQL code to select maintain several tables, set the validation and generate a hierarchical XSD. With custom SQL the corresponding XSD is always flat. That is, if you just need to insert a PGE that you could get away with custom SQL. If you need to insert a dept and EMP, I'd use TopLink.

    -C' when the intermediate abstraction of an object/array is also more logical, because you only need to import a schema relationship complex once, then generate inserts, selects, etc.

    So I hope this helps. They are each just as viable and can do something that the other cannot. Where you see an overlap I would go with your personal preference.

    Thank you

    Steve

  • by using the DCDataRow class instead of the oracle.jbo.Row interface

    Hello

    The API documentation says that class oracle.adf.model.bean.DCDataRow extends oracle.jbo.server.ViewRowImpl and implements oracle.jbo.Row.

    -In the case of a reply in another forum thread, Re: best way to recover an attributeValue of links in a backing bean?:
    ""Btw.: you can cast DCDataRow, which is the super class for data models that do not work with oracle.jbo.Row, ".
    -Also in oct2010-otn-harvest - 183714.pdf it says:
    "As I mentioned earlier, the business can be anything, casting, in which case the line type for the type of business ADF oracle.jbo.Row component may not be a generic solution to use. In this case, the type of line to DCDataRow cast
    DCDataRow vRow = (DCDataRow) vDCIteratorBinding.getCurrentRow ();
    "

    This sample application uses the DCDataRow class instead of the oracle.jbo.Row interface
    at http://www.consideringred.com/files/oracle/2010/Thread1665841AttributeValuesApp-v0.03.zip
      public String getMyNameFromIterator()
      {
        BindingContainer vBindingContainer =
           BindingContext.getCurrent().getCurrentBindingsEntry();
        DCBindingContainer vDCBindingContainer = (DCBindingContainer)vBindingContainer;
        DCIteratorBinding vDCIteratorBinding =
           vDCBindingContainer.findIteratorBinding("findSomeMyRowsIterator");
        DCDataRow vDCDataRow = (DCDataRow)vDCIteratorBinding.getCurrentRow();
        return (String)vDCDataRow.getAttribute("myName");
      }
    question
    -(q1), which would be the more detailed explanation about when and why to make a cast of the class oracle.adf.model.bean.DCDataRow instead of the oracle.jbo.Row of the interface in managed bean code?

    Thank you very much
    Jan Vervecken

    Hello

    the answer to that is that I was wrong. DCDataRow is not the super class, but one of the classes of implementation. I'll fix the entrance of the harvest with the next update. Indeed line runs through implementations

    Frank

  • Why use the symbol {} in the following script?

    Why use the symbol * {} * in the following script?
    < reading-writing-support-map-plan >
    < scheme name > SampleDatabaseScheme < / system-name >
    < internal-cache-system >
    < local plan >
    < system-Ref > SampleMemoryScheme < / plan-ref >
    < / local plan >
    < / internal-cache-system >
    < dumps-plan >
    schema < class >
    > class name < com.tangosol.examples.coherence.DBCacheStore < / class name >
    < init-params >
    < init-param >
    java.lang.String < param-type > < / param-type >
    * < {cache name} param-value > < / param-value > *.
    < / init-param >
    < / init-params >
    < / class-system >
    < / dumps-plan >
    < / reading-writing-support-map-plan >


    Thank you very much

    Published by: jetq on June 24, 2009 18:26

    Hi Frank,.

    In the example, the '{cache-name}' is supposed to be replaced by a table name or view of data that is queried for the cached data. Its purpose is show how to pass parameters to the constructor of class.

    Kind regards

    Harv

  • Why use authorization holds by updating an application?

    Why use authorization holds when updated already bought apps?

    If your method of payment changed or updated, it must be scrutinised. Your account must have the means of payment which is in order, regardless of the cost, even if it's free.

  • Why used my google chrome allows me to perform my real player downloads

    original title: why used my google chrome allows me to perform my downloads of real player, never had a problem before today

    I REINSTALLED GOOGLE CHROME ON MY LAPTOP BECAUSE THEY WOULDN'T LET ME NOT DOWN LOAD MY VIDEOS

    WELL, DID NOT HELP. WHY GOOGLE IS SO MESSED UP?

    Hello

    What version of the windows operating system is installed in the computer?

    Did you change to the computer?

    The problem is with Google chrome, I suggest you to contact Google chrome and check.

    http://support.Google.com/chrome/?hl=en

Maybe you are looking for

  • How can I remove a dial of the watch is no longer used?

    How can I remove a dial of the watch is no longer used?  I have some that I would like to delete.

  • Firefox as default browser

    Microsoft tells me I have to IEX the browser by default, because otherwise I wouldn't be able to use Windows online help in MS Office.I prefer rather Mozilla.Thus, when you open links from Outlook emails, IEX is used, with all the associated negative

  • Need drivers for motherboard for Satellite L300-1 has 3 (PSLB8E)

    I searched on the site of toshiba - Europe.com, but I can't find the drivers for my motherboard.I want to install windows xp on my laptop which came with Windows Vista... I need the driver for the motherboard? What other drivers do I need?Thanks in a

  • German version of XP on Sony laptop. Here the English version.

    My wife came back with a Sony laptop, a native of Austria, (his mother updated).  Now I want an English version.  In language change, it gives me only German or Austrian German.  I also want to upgrade the hard drive.  I have no discs for drivers Eur

  • activation / validation with vista home premium problems

    Someone at - it not a phone. to activate vista by telephone to the United Kingdom. I had to reinstall my system on a DELL pc using the original disks supplied by Dell. Since last week I've only been able to boot in safe mode. When I try a normal star