kevin Abel wrote:I asked ChatGPT to tell me the difference.
I'll look in the book to see where it has two or more classes being implemented at one time.
The interface can inherit from multiple classes.
They seem so much alike except that the interfaces can inherit from multiple classes.
How does new Worker know which method to run (if there was more than one method in the class)?
The JButton must have a class named addActionListener.
addActionListener would have a parameter to tell it which class to use.
kevin Abel wrote:
The interface looks a lot like using an abstract class and then using Extends in the class that uses it. What is the difference?
You keep using the terminology the wrong way around.
Concrete classes aren't "being implemented". They "implement". So you can look for examples of two or more classes "implementing" a common base class.
Similarly, an interface doesn't "inherit from multiple classes". A class can implement/inherit from multiple interfaces.
When you write/use an interface, you're saying: I need something that implements this contract, but I don't really care who implements it, or how they implement it.
When you write an abstract class, the primary purpose is different. You're saying: "I expect two classes to be very similar in implementation, so to make their live easier I will provide a base implementation that they can both inherit.
kevin Abel wrote:Class all_together_now Implements interface E, interface F, interface G
Similarly, an interface doesn't "inherit from multiple classes". A class can implement/inherit from multiple interfaces.
kevin Abel wrote:Couldn't we declare methods that are empty in a non-abstract class, use inheritance and overwrite the methods in the children?
We have never mentioned static classes, but that phrase is confusing and we don't use it.kevin Abel wrote:. . . Campbell's explanation about abstract classes. It cannot be run like a static class or be instantiated as an object that can be run.
You can only have empty methods if they have void instead of a return type. I think the answer to your question is, “usually no”.. . . . Couldn't we declare methods that are empty in a non-abstract class, use inheritance and overwrite the methods in the children? . . .
That&apso;s a pleasureThanks everyone,
Kevin
kevin Abel wrote:I am a chair. I exist. A movie theatre, a restaurant, a desk can utilize me and I don't care what or where I will be used.
I'm a "plant cell"
I can become a root cell OR a leaf cell OR a flower cell
Question: Why does "plant cell" have to be marked as abstract? Can't any class be inherited even without it being abstract?
I now understand how abstract works except for wondering why a non-abstract class couldn't do the same thing.
Couldn't we declare methods that are empty in a non-abstract class, use inheritance and overwrite the methods in the children?
kevin Abel wrote://Are the above lines instance variables?
Beware. I have seen vey few YouTube videos that even approach being any good. Most aren't actually wrong, but some are. Most are incomplete and assume their viewers know much more than they actually do.kevin Abel wrote:I watched a few YouTube videos . . .
kevin Abel wrote:abstract classes
You cannot instantiate them. myObj = new someAbstractclass; wont compile.
Abstract classes can have some or all of the methods be Abstract.
Another class extends the abstract class. The methods that have code between the squiggle brackets are inherited and can be used as is or they can be overwritten.
The abstract methods have to be overwritten and given a body with code between its squiggle brackets.
interface
These can not be instantiated either.
None of the methods can have a body.
one or more interfaces can be implemented in a different class
Each method has to be overwritten or the compiler will complain.
Interfaces are a contract. The classes that uses them must give a body to each of the methods.
Campbell wrote:Beware. I have seen vey few YouTube videos that even approach being any good. Most aren't actually wrong, but some are. Most are incomplete and assume their viewers know much more than they actually do.
kevin Abel wrote:Is there a way to show interface and implements in this style?
kevin Abel wrote:abstract classes
You cannot instantiate them. myObj = new someAbstractclass; wont compile.
Abstract classes can have some or all of the methods be Abstract.
Another class extends the abstract class. The methods that have code between the squiggle brackets are inherited and can be used as is or they can be overwritten.
The abstract methods have to be overwritten and given a body with code between its squiggle brackets.
interface
These can not be instantiated either.
None of the methods can have a body.
one or more interfaces can be implemented in a different class
Each method has to be overwritten or the compiler will complain.
Interfaces are a contract. The classes that uses them must give a body to each of the methods.
That question shows you are confused about nomenclature. You cannot inherit a class, but only its members. Non‑private mmbers of a class are implicitly inherited by its subclasses. You can subclass a class if it isn't labelled final and has an accessible constructor. Private constructors are not accessible to other code. Constructors don't count as members and aren't inherited by sybtypes.kevin Abel wrote:. . . Can a class without the word abstract be inherited by the subclass?
Thanks,
Kevin
This is a simplified version of this and this.Is there a way to show interface and implements in this style?
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
S Fox wrote:i think it's a good time for you to try making a GUI app with java swing and see what you can learn from that.
Carey Brown wrote:Here's a scanner that will extract those from all .java files in a given directory tree.
That question shows you are confused about nomenclature. You cannot inherit a class, but only its members. Non‑private mmbers of a class are implicitly inherited by its subclasses. You can subclass a class if it isn't labelled final and has an accessible constructor. Private constructors are not accessible to other code. Constructors don't count as members and aren't inherited by sybtypes.kevin Abel wrote:. . . Can a class without the word abstract be inherited by the subclass?
Thanks,
Kevin
This is a simplified version of this and this.Is there a way to show interface and implements in this style?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |