Showing posts with label Object Orientation. Show all posts
Showing posts with label Object Orientation. Show all posts

Friday, February 14, 2014

How do you implement multiple inheritance in JAVA?

How do you implement multiple inheritance in JAVA?
A1- First of all, Java doesn't allow you to have one class extending 2 or more classes. According to the problem you are dealing with, you can have your class to extend another class and implement many interfaces.

But let's say that you have 2 classes A and B already implemented from a library and you want  the class C to "extend from both". What can you do?

The solution A1 doesn't apply to this problem. So let's see other options:

A2- You can extend one class (e.g. C extends A) and write on your class C all non-private members from class B, redirecting them to class B members. E.g:

public class A{
  //...
}

public class B{
    public int bMehtod(int param) {  return (param + 123);   }
}

public class C extends A{
  private B b;

  public C() { this( new B() ); }
  public C(B b) {
       if ( b==null ){
           this.b = new B();
       } else {
            this.b = b;
       }
  }
  
//B members section
    public int bMehtod(int param) {  
return this.b.bMehtod(param);   //just forward the call to B
    }
}


Friday, March 30, 2012

What is POJO?

What is POJO?
POJO means Plain-Old Java Object. POJOs are "simple" Java objects to represent business logic. In contrast to classes that extend complex frameworks behaviors, such as Servlets or  EJBs, a POJO shouldn' t implement interfaces or extend abstract classes of these frameworks.

E.g.:
public class Person{
     private String name;
     public String getName() { return name; }
     public void setName(String n){ name = n; }
}

Thursday, March 29, 2012

What is the Strategy design pattern?

What is the Strategy design pattern?
The Strategy design pattern can be used when a task, a processing or an algorithm can be done in different ways, to be decided at runtime, conditioned to a context. For instance, you may have different ways of drawing your domain object, according to the choice of the user. Instead of writing some "ugly" code using a Switch block, you can program to an interface. Program to a Strategy interface.
[UML Class Diagram] Strategy design pattern
As you can see in the picture, the Context object doesn't need to know which concrete Strategy is going to be used to execute the algorithm(). You can easily add new concrete strategies in the future. The choice for the strategy to be used could be done separately from the call to  algorithm().

What is the Singleton design pattern?

What is the Singleton design pattern?
The Singleton pattern is used when you want to have only a single instance of a class in a running system.


How to implement the Singleton design pattern in Java?
There are many different ways to implement it. I will just show a simple one, that doesn't handle multi-threaded access. 
  1. Let the constructor be private;
  2. Write a public static method to return the single instance (a.k.a. "singleton"). This method checks if this is the first time the method is being called. In case positive, it creates the singleton, stores it and returns it; otherwise, it just returns the previously stored instance.
  3. Define a way to store the singleton. It could be a static variable.


Can you write a Java code snippet?
 Sure! Please check it out:

Java code snippet: Singleton design pattern

/** © 2012 interviewqa4java.blogspot.com */
public class
Singleton {

    private static
Singleton singleton = null;

    private
Singleton() {
    }

    public static Singleton getSINGLETON() {
        if
(singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    
}
}

How do I prevent classes of the other packages from accessing my members in JAVA?

How do I prevent  classes of the other packages from accessing my members in JAVA?
By using the <package-private> control acces modifier in the members. What is the keyword? None!  Please check out the example:

package ric.interviewqa4java;
public class A{
    int aField;         //visible to classes of the same package
    void aMethod(){}    //the same!
}

What is the ultimate superclass in Java?

What is the ultimate superclass in Java?
The class Object.  All classes in Java that don't explicitly extend another class will implicitly extend this class.

Tuesday, January 17, 2012

What is the difference between Abstract Class and Interface?

What is the difference between Abstract Class and Interface?

Abstract class can have method implementation; interface can't. A class can implement many interfaces, but a class can only extend one Abstract class. Interface fields are constants (static final); Abstract class aren't: their values can be updated.

These are some differences I remember. Please add a comment if you find another one.