abstract

How is abstract class different from concrete class?

二次信任 提交于 2019-11-28 16:02:48
问题 I understand WHY we need Abstract Class in Java - to create sub-classes. But the same can be achieved by concrete class. e.g. Class Child extends Parent. Here Parent can very well be abstract & concrete. So why do we have ABSTRACT?? 回答1: Abstract classes cannot be instantiated directly. Declaring a class as abstract means that you do not want it to be instantiated and that the class can only be inherited. You are imposing a rule in your code. If you extend your Parent/Child relationship

abstract class and anonymous class [duplicate]

别说谁变了你拦得住时间么 提交于 2019-11-28 14:00:16
This question already has an answer here: Creating the instance of abstract class or anonymous class 8 answers abstract class Two { Two() { System.out.println("Two()"); } Two(String s) { System.out.println("Two(String"); } abstract int display(); } class One { public Two two(String s) { return new Two() { public int display() { System.out.println("display()"); return 1; } }; } } class Ajay { public static void main(String ...strings ){ One one=new One(); Two two=one.two("ajay"); System.out.println(two.display()); } } we cannot instantiate an abstract class then why is the function Two two

Run-time Polymorphism in Java without “abstract”?

半城伤御伤魂 提交于 2019-11-28 12:03:14
问题 I was going over the official Oracle tutorial where it introduces the idea of polymorphism with the example of a class hierarchy of 3 classes; Bicycle being the superclass, and MountainBike and RoadBike being 2 subclasses. It shows how the 2 subclasses override a method "printDescription" declared in Bicycle, by declaring different versions of it. And finally, toward the end, the tutorial mentions the Java Virtual Machine (JVM) calls the appropriate method for the object that is referred to

Why can't we instantiate an interface or an abstract class in java without an anonymous class method?

青春壹個敷衍的年華 提交于 2019-11-28 08:42:56
I know, we can not instantiate either an interface or an abstract class in java except using anonymous class method but what is the reason behind it? You can't instantiate an interface or an abstract class because it would defy the object oriented model. Interfaces represent contracts - the promise that the implementer of an interface will be able to do all these things, fulfill the contract. Abstract classes are a similar idea, in that they represent an unfulfilled contract, a promise to be able to do things, except unlike interfaces they have some of their functions or fields defined but

why do we need abstract classes in Java? [closed]

随声附和 提交于 2019-11-28 08:21:37
Why do we need abstract classes in Java? If you're never going to make it into an object, why have it in the first place? How do you use it? Why is it there? I'm wondering the same thing with abstract methods. I find it seems like a similar concept to having a super class with NO subclasses that could ever matter to be made. Scary Wombat An abstract class can be used as a type of template for other classes. The abstract class will hold common functionality for all classes that extend it. For example: Abstract Class Animal All animals move and breathe and reproduce so these can be put into the

Can an enum have abstract methods?

大憨熊 提交于 2019-11-28 07:58:55
Can an enum have abstract methods? If so, what is the use, and give a scenario which will illustrate this usage. lukastymo Yes, but you may prefer enum which implements an interface, Look here . I think It looks much better. This is example for abstract method: public enum Animal { CAT { public String makeNoise() { return "MEOW!"; } }, DOG { public String makeNoise() { return "WOOF!"; } }; public abstract String makeNoise(); } Yes, you can define abstract methods in an enum declaration if and only if all enum values have custom class bodies with implementations of those methods (i.e. no

Scala client composition with Traits vs implementing an abstract class

风流意气都作罢 提交于 2019-11-28 06:15:57
I have read that with Scala, it is generally advised to use Traits instead of Abstract classes to extend a base class. Is the following a good design pattern and layout? Is this how Traits were intended to replace Abstract? client class (with def function1) trait1 class (overrides function1) trait2 class (overrides function1) specificClient1 extends client with trait1 specificClient2 extends client with trait2 Travis Brown I don't know what your source is for the claim that you should prefer traits over abstract classes in Scala, but there are several reasons not to: Traits complicate Java

Instantiating interfaces in Java

喜你入骨 提交于 2019-11-28 04:42:32
I have this interface: public interface Animal { public void Eat(String name); } And this code here implements the interface: public class Dog implements Animal { public void Eat(String food_name) { System.out.printf(food_name); } public static void main(String args[]) { Animal baby2 = new Dog(); //HERE!!!!!!!!!!!!!!!!!!!!!! baby2.Eat("Meat"); } } My question is, why does the code work? An interface cannot be instantiated. Yet in this case, interface was instantiated (marked with the comment "HERE!!!!!!!!!!!!!"). What is happening here? Ziyao Wei No it is not - you are instantiating a Dog ,

C++ Abstract Class: constructor yes or no?

陌路散爱 提交于 2019-11-28 03:15:14
A class with one (or more) virtual pure functions is abstract, and it can't be used to create a new object, so it doesn't have a constructor. I'm reading a book that provides the following example: class Employee { public: Employee(const char*, const char*); ~Employee(); const char* getFirstName() const; const char* getLastName() const; virtual double earnings() const=0 // pure virtual => abstract class virtual void print() const private: char* firstName, lastName; }; If the class is abstract why we have a constructor? It uses this class later ( Boss is public derived from Employee ): void

Use of an abstract class without any abstract methods

孤街浪徒 提交于 2019-11-28 03:10:04
问题 An abstract class need not include any abstract methods. Is there any other reason to make a class abstract other than the fact that abstract classes can't be instantiated? 回答1: The principal role of an abstract class is to provide an appropriate root class from which concrete, (i.e. non-abstract) subclasses can be derived. This is a powerful and versatile feature which promotes code re-use. Abstract classes encapsulate general features that are common to a range of data types - features