Example program for Inheritance

Ezzaral commented: Google -3

Recommended Answers

All 6 Replies

class A{}

class B extends A{}

through this way, class B inherits all the methods inside the A class, and B would pass a IS-AN-A test, so polymorphism would be possible.

This is the second thread of yours (after overloading) I've answered, but even though I have given (very) short yet correct answers, I've give no information Google the Wise wouldn't have been able to give you.

if you actually want to learn about this stuff, do yourself a huge favour and put some effort in it, don't expect to be handfed, because even if you expect that, you'll still need to learn to crawl before you'll ever be able to walk.

I hope you have basics in Java.if not, you can check here to learn Java basics.

Now remember that Java does not support multiple inheritance.You inherit a class characteristics by using java keyword extends.

If you want to perform multiple inheritance,Java provides this by use of interfaces.

Check the code below on inheritance

public class Computer
{
private String model;
public String getModel(String model)
{
this.model = model;
return model;
}
}
Create another class that extends Computer.
public class myTest extends Computer
{
//provide a String for method getModel().
//maybe set model = "Compaq" etc.
//This class extends methods defined in class Computer.
}
If you have any question just post it.

I hope you have basics in Java.if not, you can check here to learn Java basics.

Now remember that Java does not support multiple inheritance.You inherit a class characteristics by using java keyword extends.

If you want to perform multiple inheritance,Java provides this by use of interfaces.

Check the code below on inheritance

public class Computer
{
private String model;
public String getModel(String model)
{
this.model = model;
return model;
}
}
Create another class that extends Computer.
public class myTest extends Computer
{
//provide a String for method getModel().
//maybe set model = "Compaq" etc.
//This class extends methods defined in class Computer.
}
If you have any question just post it.

there is NO multiple inheritance in Java. Implementing an interface is not the same as inheritance, for instance, you don't 'inherit' methods, it just forces the developer of the class that implements the interface to implement certain methods. the class still has to decide on the logic therein itself.

Yah somehow argued it is true. But interfaces is a way in which java tries to implement multiple inheritance.Thanks for clarification.

actually, they very clearly decided not to support multiple inheritance.
it could lead to problems, for instance:

class A has a method writeData()
class B and C both extend class A, and they both overwrite the writeData() method

so far, there is no problem, but now comes the problem ... the next is not possible in Java, and this is the main reason there's no multiple inheritance support.

class D extends both class B and class C. if multiple inheritance would have been allowed, how on earth would the compiler know what writeData() method should be used in class D, unless it overwrites it itself?

inheritance suggests that, except for extending the class you don't have to do anything to use the methods and such in the parent class except if you want to change the logic or behaviour in a method, then you'll need to overwrite it, while an interface only provides a sort of 'skeleton' which decides what methods the class must implement, but it doesn't say how it should implement it, or how or what the code should do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.