Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • General CS Concepts

    by Published on March 8th, 2011 04:05 AM
    1. Categories:
    2. General CS Concepts

    Interfaces

    Interfaces are also known as purely abstract classes: they contain only abstract methods. An abstract method is one that has not been implemented, but defines how that method is called and what to expect back as output. It is the job of inheriting classes to implement the abstract methods.

    A quick note about interfaces: while Java doesn't allow you to have one class that inherits from multiple sources, that one class can implement multiple interfaces. There are reasons for this, but I won't discuss it here (personally, I wish Java did allow multiple inheritance and got rid of the interfaces, but that's just me).
    ...
    by Published on March 8th, 2011 04:05 AM
    1. Categories:
    2. General CS Concepts

    Another note to interfaces.

    When you have an interface declared like this.

    public interface Doable
    {
         int myInt = 10;
         boolean canDoIt1();
         int doIt2(int num);
    }

    The code will implicitly look like this.

    public interface Doable
    {
         [b]public static final[/b] int myInt = 10;
         [b]public[/b] boolean canDoIt1();
         [b]public[/b] int doIt2(int num);
    }
    ...
    by Published on March 8th, 2011 04:04 AM
    1. Categories:
    2. General CS Concepts

    Inheritance

    The concept of inheritance is similar to inheritance in real life: the child gets what the parent has. Also, the child still has what the child had, and can more-or-less do what it wants with what the parent had.

    In computer science, there are some limitations. Remember declaring things private or protected, and thinking to yourself "these two keywords look like they're doing the same thing!"? Inheritance is why they're there. Private things are not given to the child, but protected and public are. There's not really a good analogy that I can think of for this, but hopefully it's not too hard to remember.
    ...
    by Published on March 8th, 2011 04:03 AM
    1. Categories:
    2. General CS Concepts

    Encapsulation

    Encapsulation is a similar idea to Abstraction. It deals with restricting access to components of an object. A good analogy would be airport security. You're not allowed to bring certain things in (weapons), and you're not allowed to take (steal) certain things from the airport.

    A programming example of this would be this:

    public class ComputeSqrt
    {
         private double number;
     
         public ComputeSqrt(double number)
         {
              setNumber(number);
         }
     
         public void setNumber(double number)
         {
              if (number >= 0)
              {
                   this.number = number;
              }
         }
     
         public double getNumber()
         {
              return this.number;
         }
     
         public double computeSqrt()
         {
              return Math.sqrt(this.number);
         }
    }
    ...
    by Published on March 8th, 2011 04:02 AM
    1. Categories:
    2. General CS Concepts

    Abstraction

    Abstraction is the concept of hiding away the details. It can basically be summed up with this phrase: I don't care how it happens, just get it done.

    There are many ways to abstract away the details. The main way is through methods/functions, or objects. Here's an example:

    Let's say you want to test if a number is prime or not. How would you do this? You could execute this code:
    int number = ...; // number you want to test if prime
    boolean isPrime = true;
    if (number < 2 || (number % 2 == 0 && number != 2))
    {
         isPrime = false;
    }
    for (int i = 3; i < number && isPrime; i++)
    {
         if (number % i == 0)
         {
              isPrime = false;
         }
    }
    ...
    by Published on March 8th, 2011 04:01 AM
    1. Categories:
    2. General CS Concepts

    This is a post for general CS concepts. To make it easy to link to, i've posted each different concept under their own post. The emphasis will be on OOP (object oriented programming), and examples will either be in Java or some java-pseudo code. At the bottom of each section, i've put some simple questions that should test your understanding of the topic. Try to see how many you can get right. The answers are below them (in a slightly hidden color), just highlight the text to read the answer.
    ...
    Page 2 of 2 FirstFirst 12