Understanding Java Class Designs

When a method is the same name as the class. When you instantiate a class, the 'constructor' is automatically executed
November 7, 2013
OCPJP-7

Storing arguments when instantiating classes

You can use the word 'this', or use another name.

class Car {
private int wheels, seats;
private String price;

public Car(int wheels, int seats, String price){
this.wheels = wheels;
this.seats = seats;
this.price = price;
}


or...

class Car {
private int noOfWheels, noOfSeats;
private String priceOfCar;

public Car(int wheels, int seats, String seats){
noOfWheels = wheels;
noOfSeats = seats;
priceOfCar = price;
}


A constructor is NOT a constructor when...

...you have a return type. the method is not a constructor when it has a return type (it is however still a valid method)

class Car{
public void Car(){

}
}


Access Modifiers

Where you can enforce effective encapsulation

  • 1. Access within the class
  • 2. Subclass inside the package/li>
  • 3. Subclass outside the package/li>
  • 4. Other class inside the package/li>
  • 5. Other class outside the package/li>

Your options:
  • public - (1,2,3,4,5) Can be access by anything
  • private - (1 only) Used within the class (not accessible outside the class)
  • protected - (1,2,3,4) Just like public, though classes outside the package cannot access it


Fundamentals of OOP

Foundations of OOP

  • Abstraction -- 'Taking away' the complicated processes thus leaving only the high-level
  • Encapsulation -- combining everything together function + values = Class
  • Inheritance -- You 'inherit' the commonalities from a superclass, saves having to re-write the code again
  • Polymorphism -- Calling from a number of methods (all with the same name) though one method will be selected based on the arguments passed


Overloading Methods

Where you can enforce effective encapsulation

You can have methods with the same name though the arguments are different.
Example: When you instantiate a class and pass only 2 arguments (wheels and seats), it will know to pick the bottom method.

class Car {
private int wheels, seats;
private String price;

public Car(int wheels, int seats, String price){
//Stuff here
}
public Car(int wheels, int seats){
//Stuff here
}


Determining overloading

If will also determine the type of argument

byte b = 10;
Integer i = 10;

value(b); //passing the byte
value(i); //passing an Integer (not an int)
value("10"); //passing a String


public static void price(byte b){System.out.println("This is a byte")}
public static void price(int b){System.out.println("This is an int")}
public static void price(Object b){System.out.println("This is an object")}
public static void price(String b){System.out.println("This is a String")}

It would return:
This is a byte
This is an object
This is a string

Even though you pass an Integer value, there's no method expecting an Integer type, so it looks up the branch of inherited classes.
[Object]
 [Number]
  [int]
  [short]
  [double]
  [byte]


Overloading will fail when...

You call a method, with arguments, though the argument types don't match any of the values

private static void price(int x, int y);
private static void price(int x, String y);
price("Hello","Daniel");


Another overload example

class AnotherOverload { public static void anotherMethod(byte val) { System.out.println("this is a byte");
public static void anotherMethod(short val) { System.out.println("this is a short");
}


Using the 'this()' method

You use the this keyword to call one constructor from another constructor of the same class

Public Fruit (int x, String y, String z){
 size = x;
 texture = y;
 shape = z;
}

Public Fruit (int x, String y){
 this(x , y,"Round");
}


Number class

Object is the superclass. A new instance of the Number class can be used to hold derived type objects. Number [] num = new Number[2];
num[0] = new Byte((Byte)10);
num[1] = new Integer(10);

System.out.println(sum(num));


Overriding Issues

The access level for the toString() method must be Public

public String toString() { return "x =" + x; }

I cannot be anything more stricter.

protected String toString() { return "x =" + x; }

  • The return of the toString returns return
  • public String toString() { return "x =" + x; }

    The toString() method returns a string

    protected String toString() { return "x =" + x; }


    Convariant return types

    Return type of the methods should exactly match when overriding methods. In the example below, the Circle c2 = c1.copy is trying to run the method within the Circle, though the method is an overriding method from the Super class.


    +-+Shape
    +-+copy()

    +-+Circle Shape
    +-+public Shape copy()

    +-+ Test
    +-+ new Circle(), Circle c2 = c1.copy

    This requires explicit downcasting to the derived class.
    Circle c2 = (Circle) c1.copy
    Though this is no longer required to do as Circle c2 = c1.copy compiles successfully within Java 5

    About the author

    Daniel is a Technical Manager with over 10 years of consulting expertise in the Identity and Access Management space.
    Daniel has built from scratch this blog as well as technicalconfessions.com
    Follow Daniel on twitter @nervouswiggles

    Comments

    Other Posts

    AWS-PHP integration - Email not sent. SMTP Error: Could not authenticate.

    phpsmtpaws

    February 6, 2020
    Created by: Daniel Redfern
    AS I was migrating my environment into an S3 environment, I wanted to leverage off the SES services that AWS provide, more specifically, to leverage the off the SMTP functionality by sending an email via PHP
    Read More...

    SOLUTION: no headers files (.h) found in softwareserial - Arduino

    Arduino

    February 24, 2019
    Created by: Daniel Redfern
    The WeMos D1 is a ESP8266 WiFi based board is an extension to the current out-of-the-box library that comes with the Arduino installation. Because of this, you need to import in the libraries as well as acknowledging the specific board. This process is highly confusion with a number of different individuals talking about a number of different ways to integrate.
    Read More...

    NameID element must be present as part of the Subject in the Response message

    ShibbolethSAML

    August 7, 2018
    Created by: Daniel Redfern
    NameID element must be present as part of the Subject in the Response message, please enable it in the IDP configuration.
    Read More...

    HOW TO provision AD group membership from OpenIDM

    OpenIDMICFAD-connector

    June 15, 2018
    Created by: Daniel Redfern
    For what I see, there's not too many supportive documentations out there that will demonstrate how provision AD group membership with the ICF connector using OpenIDM. The use of the special ldapGroups attribute is not explained anywhere in the Integrators guides to to the date of this blog. This quick blog identifies the tasks required to provision AD group membership from OpenIDM to AD using the LDAP ICF connector. However this doesn't really explain what ldapGroups actually does and there's no real worked example of how to go from an Assignment to ldapGroups to an assigned group in AD. I wrote up a wiki article for my own reference: AD group memberships automatically to users This is just my view, others may disagree, but I think the implementation experience could be improved with some more documentation and a more detailed example here.
    Read More...

    ForgeRock OpenIDM - InvalidCredentialException: Remote framework key is invalid

    ICFIDMOpenIDMOpenICF

    November 8, 2017
    Created by: Daniel Redfern
    In the past, the similar error occurred though for the Oracle Identity Management solution. invalidcredentialexception remote framework key is invalid Because they all share the ICF connector framework, the error/solution would be the same.
    Read More...

    org.forgerock.script.exception.ScriptCompilationException: missing ; before statement

    IDMsync.confforgerockopenidm

    November 8, 2017
    Created by: Daniel Redfern
    org.forgerock.script.exception.ScriptCompilationException: missing ; before statement
    Read More...

    ForgeRock IDM - org.forgerock.script.exception.ScriptCompilationException: missing ; before statemen

    OpenIDMsync.confForgeRock

    September 17, 2017
    Created by: Daniel Redfern
    ForgeRock IDM - org.forgerock.script.exception.ScriptCompilationException: missing ; before statement
    Read More...

    Caused by: org.forgerock.json.resource.BadRequestException: Target does not support attribute groups

    OpenIDMForgeRockICFConnector

    September 17, 2017
    Created by: Daniel Redfern
    When performing the attempt of a reconciliation from ForgeRock IDM to Active Directory, I would get the following error
    Read More...

    ForgeRock OpenIDM - InvalidCredentialException: Remote framework key is invalid

    OpenIDMForgeRockICFConnectorAD

    September 17, 2017
    Created by: Daniel Redfern
    In the past, the similar error occurred though for the Oracle Identity Management solution. invalidcredentialexception remote framework key is invalid Because they all share the ICF connector framework, the error/solution would be the same.
    Read More...

    ERROR Caused by com.google.api.client.auth.oauth2.TokenResponseException 400 Bad Request - invalid_g

    OpenIDMIDMGoogleGoogle-AppsICFreconciliation

    September 12, 2017
    Created by: Daniel Redfern
    During the reconcilation from OpenIDM to the ICF google apps connector, the following error response would occur. ERROR Caused by com.google.api.client.auth.oauth2.TokenResponseException 400 Bad Request - invalid_grant
    Read More...