max
max

Reputation: 11

Inheritance concept java..help

I'd be very grateful if someone could help me to understand the inheritance concept in Java. Is the following code an example of that?

I mean the class WavPanel is actually a subclass of JPanel which acts as a superclass.

Is that correct?

If so it means that "what JPanel has, also WavPanel but it is more specific since through its methods you can do something else".

Am I wrong?

thank you. Max

import javax.swing.JPanel;
class WavPanel extends JPanel {

        List<Byte> audioBytes;
        List<Line2D.Double> lines;

        public WavPanel() {
            super();
            setBackground(Color.black);
            resetWaveform();
        }

        public void resetWaveform() {
            audioBytes = new ArrayList<Byte>();
            lines = new ArrayList<Line2D.Double>();
            repaint();
        }
}

Upvotes: 1

Views: 286

Answers (5)

Riana
Riana

Reputation: 41

Inheritance concept is often used as a way to add new attributes or operations to an existing class, but the most important feature is truly polymorphism as explained by Andreas. Lots of GOF design pattern (for instance T*emplate Method*, State, Strategy...) are based on polymorphism.

To resume, inheritance isn't only for structural extension, but also for dynamic behavior implementations. For instance, check out the Strategy design pattern from the link i provided above. Despite the samples are in C#, the most important thing is to understand the philosophy. If you want to read more about object oriented principles, you should have a look at the General Responsibility Assignment Software Principles (or Patterns), abbreviated GRASP.

Upvotes: 0

LuckyLuke
LuckyLuke

Reputation: 49097

It also means that you could assign

JPanel mypanel = new WavPanel();

since you extend the JPanel with custom/more functionality, and why? Because WavPanel IS A JPanel. However you can not invoke methods that are implemented in WavPanel class.

This concept is called polymorphism.

If you had 3 different classes that all inherit methods/attributes from a superclass named Animal that provided general methods, say 'Run', 'Walk', 'Eat' and so on you could process different object types in a for loop because the animals that extend this 'Animal' class IS A Animal.

Dog myDog = new Dog();
Horse myHorse = new Horse();
Cat myCat = new Cat();


ArrayList<Animal> myAnimals = new ArrayList<Animal>();

myAnimals.add(myDog);
myAnimals.add(myHorse);
myAnimals.add(myCat);

for (Animal animal : myAnimals) {
    animal.run();
} 

This example assume you have made an Animal class though.

Upvotes: 0

Matt
Matt

Reputation: 5704

You appear to have a fairly good understanding of inheritance directly from its definition. The subclass of a class does, in fact, have all of the methods and data members of its superclass. However, they are not the same because the subclass has more specific functions and data members that accomplish a more specific task that the superclass. (Just to rephrase what you said as a generalization for all classes.)

Though you do seem to just feel like you don't understand it on a casual level. There are plenty of online resources that teach by example such as here:

What's a good example for class inheritance?

Upvotes: 0

Bozho
Bozho

Reputation: 597382

You are right. WavPanel is a JPanel. But an ordinary JPanel would not have any components in it. That's why you extend it, so that you can add some behaviour specific to the WavPanel

Upvotes: 2

user1036322
user1036322

Reputation: 1

Yes, when you extend that means you take that class, and are able to add more attributes to it. Also, the superclass for all objects is Object.

Upvotes: 0

Related Questions