public class hello_world {



        public void sample(){

            System.out.println("Hello World");


        }
    };

  /*Now I can access this method using an Instance of it in my Main Class or by inheriting it.*

  Public Class main{

  public static void main(String args[]){

        hello_world sam = new hello_world();
        sam.sample();

    };

  }

    /*Class using inheritance

    Public Class main extends hello_world {

  public static void main(String args[]){

        sample();

    };

Using inheritance and instance I can achieve the same result.

Can someone let me know as to whats is the benefit of using and Instance and benefit of using inheritance.

first of all, the code you have above wouldn't work. your "inheritance" example would only work if: either you create an instance of that class, or you declare the sample() method as static.

since your point isn't correct to begin with (for all the non-static methods, you'll still need an instance) so answering this question is a bit... pointless.

inheritance and instantiating are both OO concepts, very basic ones at that, but they don't (can't, in fact) replace each other.

the best thing for you to do now, is to read up on both inheritance and instantiation, because it seems to me you don't truly understand what they are/do. I would suggest you start with the three links I added below.

Inheritance

Instance vs class members

Creating objects (contains explanation about instantiating.)

You should read stultuske links. They will help you to understand the language. There are a lot more involved in the reason.

To give you a short answer, a class can inherit from only 1 class at a time. You cannot inherit more than 1 class unless you inherit from a class again.

class A1 { ... }
class A2 { ... }

class B extends A1 { ... }  // OK

class C extends A2 { ... }  // OK

class D extends A1, A2 { ... }   // cannot do this

If you want to inherit a class, you must ensure that your class really needs all properties/features of the class you inherit from.

you must ensure that your class really needs all properties/features of the class you inherit from.

I tend to disagree. why do you have to ensure that?

To me, it is for the full efficiency. When you inherit a class, the whole super class structure will be loaded to the memory including your add on. Maybe not the whole, but you may consider how it would be when your class becomes parent, grand parent, great grand parent, etc., of other classes. If certain features/properties are that common, I may try to implement them as static.

well, yes, but that's not what you said. you said "you have to make sure you need all of the properties/features"

now, look at this generic example

public class A{
  public void methodA(){
     // now this is a logic thing for class A to have, but for any other (even subclass of A) class, it would be pointless
  }

  public void inheritThis(){
    // especially for this method, we want to inherit to ChildClass
  }
}

now, according to you, if we inherit A, we have to make sure we need all the methods in A. But let's say ChildClass does need the inheritThis method (and presume it's a piece of logic, thousands of lines of code and method calls, anyway, too much to re-write), but ChildClass may NOT offer the functionality of methodA.

public class ChildClass extends A{
  @Override
  public void methodA(){
    throw new UnsupportedOperationException("Stick to the inheritThis method!!");
  }
}

I guess I might have used too strong word in my statement. Well, I am sorry for saying that then.

if we inherit A, we have to make sure we need all the methods in A. But let's say ChildClass does need the inheritThis method (and presume it's a piece of logic, thousands of lines of code and method calls, anyway, too much to re-write), but ChildClass may NOT offer the functionality of methodA.

Does that mean call A won't be loaded to the memory? I guess not... That's why I said if certain featuers/properties are so common in a class, I would rather attempt to implement them as static instead of go the easy way -- inherit. :)

problem is, sometimes it does come in handy to have 'm instance based :)

Don't confuse methods and variables. The compiled code for all methods will be loaded along with the class some time prior to the class's first use. The memory for static variables ditto. The memory for instance variables is allocated when a new instance is created. All this is true of each class and its superclasses.
The choice of static or not is nothing to do with efficiency - the program logic will require that a variable should belong to the class or have one value for each instance.

OK :) Thanks for the clarification.

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.